WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JiepeiWMS</title>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
    <style>
        div {
            margin: 10px;
            word-wrap: break-word;
        }
    </style>
    <script>
 
        $(document).ready(function () {
            $("#jsonp").click(function () {
 
                $.getJSON("http://localhost:8081/api/Login/jsonp?callBack=?", function (data) {
                    $("#data-jsonp").html("数据: " + data.value);
                });
            });
 
            $("#cors").click(function () {
                $.get("http://localhost:8081/api/Login/Token", function (data, status) {
                    console.log(data);
                    $("#status-cors").html("状态: " + status);
                    $("#data-cors").html("数据: " + data? data.token:"失败");
                });
            });
 
            $("#cors-post").click(function () {
                let postdata = {
                    "bID": 10,
                    "bsubmitter": "222",
                    "btitle": "33333",
                    "bcategory": "4444",
                    "bcontent": "5555",
                    "btraffic": 0,
                    "bcommentNum": 0,
                    "bUpdateTime": "2018-11-08T02:36:26.557Z",
                    "bCreateTime": "2018-11-08T02:36:26.557Z",
                    "bRemark": "string"
                };
                $.ajax({
                    type: 'post',
                    url: 'http://localhost:8081/api/Values',
                    contentType: 'application/json',
                    data: JSON.stringify(postdata),
                    success: function (data, status) {
                        console.log(data);
                        $("#status-cors-post").html("状态: " + status);
                        $("#data-cors-post").html("数据: " + JSON.stringify(data));
                    }
                });
 
 
 
                //$.ajax({
                //    type: "POST",
                //    url: "/api/Values",
                //    success: function (data, status) {
                //        console.log(data);
                //        $("#status-cors-post").html("状态: " + status);
                //        $("#data-cors-post").html("数据: " + data);
                //    }
                //});
 
 
            });
 
        });
    </script>
</head>
<body>
 
    <h3>通过JsonP实现跨域请求</h3>
    <button id="jsonp">发送一个 GET </button>
 
    <div id="status-jsonp"></div>
    <div id="data-jsonp"></div>
    <hr />
 
    <h3>添加请求头实现跨域</h3>
    无
    <hr />
 
 
    <h3>通过CORS实现跨域请求,另需要在服务器段配置CORE</h3>
    <button id="cors">发送一个 GET </button>
 
    <div id="status-cors"></div>
    <div id="data-cors"></div>
    <hr />
    <button id="cors-post">发送一个 POST </button>
 
    <div id="status-cors-post"></div>
    <div id="data-cors-post"></div>
    <hr />
</body>
</html>