-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.html
91 lines (90 loc) · 3.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="src/XMan.js"></script>
<script>
// 请提前配置host本机127.0.0.1为cross.domain.com
// 当前页面是通过http://localhost:63342/index.html访问
window.onload = function () {
// jsonp用法一
x.jsonp('http://cross.domain.com:3000/jsonp', {type: 'jsonp'}, 'cb', function (data) {
console.log('[LOG] type:jsonp1,data: ' + JSON.stringify(data));
});
// jsonp用法二
x({
method: 'get',
url: 'http://cross.domain.com:3000/jsonp',
type: 'jsonp',
data: {type: 'jsonp'},
callbackName: 'cb',
success: function (data) {
console.log('[LOG] type:jsonp2,data: ' + JSON.stringify(data));
}
});
// 跨域资源共享用法一
x.crossDomain('post', 'http://cross.domain.com:3000/cors', {type: 'cors'}, function (data) {
console.log('[LOG] type:cors1,data: ' + JSON.stringify(data));
});
// 跨域资源共享用法二
x({
method: 'post',
type: 'crossDomain',
url: 'http://cross.domain.com:3000/cors',
data: {type: 'cors'},
success: function (data) {
console.log('[LOG] type:cors2,data: ' + JSON.stringify(data));
},
withCredentials: true,
headers: {'mySelf': 'byMyself'}
});
// form request用法一
x.formRequest('post', 'http://cross.domain.com:3000/form', {type: 'form'}, function (data) {
console.log('[LOG] type:form1,data: ' + JSON.stringify(data));
});
// form request用法二
x({
method: 'post',
type: 'formRequest',
url: 'http://cross.domain.com:3000/form',
data: {type: 'form'},
success: function (data) {
console.log('[LOG] type:form2,data: ' + JSON.stringify(data));
}
});
// iframe 跨域 通讯 用法一
var outer = x.frame(window.frames[0]);
outer.on('triggerOuter', function (data) {
console.log('[LOG triggerOuter] type:frames,data: ' + JSON.stringify(data))
});
outer.on('Message', function (data) {
console.log('[LOG Message Outer] type:frames,data: ' + JSON.stringify(data))
});
setTimeout(function () {
outer.emit('triggerInner', {from: 'outer'});
outer.send('this msg from outer');
}, 500);
// iframe 跨域 通讯 用法二
var outer2 = x({
method: 'get',
type: 'frame',
targetWindow: window.frames[0]
});
outer2.on('triggerOuter', function (data) {
console.log('[LOG triggerOuter2] type:frames,data: ' + JSON.stringify(data))
});
outer2.on('Message', function (data) {
console.log('[LOG Message Outer2] type:frames,data: ' + JSON.stringify(data))
});
setTimeout(function () {
outer2.emit('triggerInner', {from: 'outer2'});
outer2.send('this msg from outer2');
}, 600);
};
</script>
</head>
<body>
<iframe src="http://cross.domain.com:3000/inner.html"></iframe>
</body>
</html>