-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
111 lines (95 loc) · 3.28 KB
/
index.html
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
101
102
103
104
105
106
107
108
109
110
111
<!-- 執行 php -S localhost:8001 啟動php內建server在localhost:8001 port -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<input type="button" value="JSON" onclick="runJSON()">
<p></p>
<input type="button" value="runJQueryJSONP" onclick="runAjax()">
<p></p>
<input type="button" value="runRawJSONP" onclick="runJSONP()">
<p></p>
<input type="button" value="AjaxCrossDomainParameter" onclick="jsonpCross()">
<p></p>
<input type="button" value="Xss" onclick="jsonpXss()">
</body>
<script>
(function() {
var row1 = {'URL':'http://store.company.com/dir2/other.html', 'Outcome':'同源', 'Reason':''};
var row2 = {'URL':'http://store.company.com/dir/inner/another.html', 'Outcome':'同源', 'Reason':''};
var row3 = {'URL':'https://store.company.com/secure.html', 'Outcome':'不同源', 'Reason':'協定不同'};
var row4 = {'URL':'http://store.company.com:81/dir/etc.html', 'Outcome':'不同源', 'Reason':'埠號不同'};
var row5 = {'URL':'http://news.company.com/dir/other.html', 'Outcome':'不同源', 'Reason':'主機位置不同'};
console.table([row1, row2, row3, row4, row5,]);
})();
// 使用jQuery ajax api做到跨domain
function runAjax() {
$.ajax({
url: 'http://localhost:8002/index.php',
type: 'GET',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'handler',
success: function(jsonp) {
console.log(jsonp);
}
});
}
// 使用jQuery ajax api做到跨domain,參數使用crossDomain, 免去打jsonp & jsonCallback參數
function jsonpCross()
{
$.ajax(
{
url: 'http://localhost:8002/index.php',
dataType: 'jsonp',
crossDomain: true
})
.done(function(data)
{
console.log(data) ;
}) ;
}
// 原生javascript做到跨domain
// 要建立全域 var handle,否則runJSONP()內看不到handle ;
var handle = function(jsonp) {
console.log(jsonp);
};
function runJSONP() {
var url = "http://localhost:8002/index.php?callback=handle";
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}
//同源JSON資料取得,若server開啟header('access-control-allow-origin:*'); 則允許跨domain
function runJSON() {
$.ajax({
url: 'http://localhost:8002/index.php',
type: 'GET',
dataType: 'json',
success: function(json) {
console.log(json);
}
});
}
function jsonpXss()
{
$.ajax(
{
url: 'http://localhost:8002/xss.php',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'handler'
})
.done(function(data)
{
console.log(data) ;
}) ;
}
</script>
</html>