forked from colorhook/crossdomain
/
index.html
executable file
·59 lines (56 loc) · 1.48 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>crossdomain | JSONP</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<style>
body, button{
font-family: Verdana, Arial;
color: #333;
}
h1{
text-align:center;
font-size:24px;
}
</style>
</head>
<body>
<h1>crossdomain using JSONP</h1>
<button id='get_resource'>Click to get resource</button>
<p id='resource_holder'></p>
<script>
(function(){
var getJSONP = function(url, callbackName){
var head = document.getElementsByTagName("head")[0],
script = document.createElement("script");
script.src = url + '?callback=' + callbackName;
script.charset = "utf-8";
script.onload = script.onreadystatechange = function(){
if (!this.readyState || this.readyState == "loaded" ||
this.readyState == "complete") {
setTimeout(function(){
head.removeChild(script);
}, 50);
}
};
head.appendChild(script);
},
holder = document.getElementById('resource_holder'),
button = document.getElementById('get_resource');
window.handleJSONP = function(data){
var dumpInfo = 'timestamp: ' + data.timestamp + '<br/>count: ' + data.count,
record = data.data;
for(var i in record){
dumpInfo += '<br/>record ' + i + ': {id: ' + record[i].id + ' name: ' + record[i].name +'}';
}
holder.innerHTML = dumpInfo;
}
button.onclick = function(){
holder.innerHTML = '';
getJSONP('http://www.b.com/crossdomain/jsonp/jsonp.php', 'handleJSONP');
}
})();
</script>
</body>
</html>