-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLHttpRequest.html
131 lines (124 loc) · 4.58 KB
/
XMLHttpRequest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XMLHttpRequest</title>
</head>
<body>
<button id="request">request</button>
<script>
// 创建XMLHttpRequest
function getHTTPObject() {
if (typeof XMLHttpRequest == "undefined") {
XMLHttpRequest = function() { //兼容ie
try {
return new ActiveXObject("Msxnl2.XMLHTTP.6.0");
} catch (e) {}
try {
return new ActiveXObject("Msxnl2.XMLHTTP.3.0");
} catch (e) {}
try {
return new ActiveXObject("Msxnl2.XMLHTTP");
} catch (e) {}
return false;
}
}
return new XMLHttpRequest();
}
//创建及发送
function xmlHttp(method, url, data, callback, failback) {
var request;
//创建XMLHttpRequest
if (typeof XMLHttpRequest == "undefined") {
XMLHttpRequest = function() { //兼容ie
try {
request = new ActiveXObject("Msxnl2.XMLHTTP.6.0");
} catch (e) {}
try {
request = new ActiveXObject("Msxnl2.XMLHTTP.3.0");
} catch (e) {}
try {
request = new ActiveXObject("Msxnl2.XMLHTTP");
} catch (e) {}
return false;
}
}
request = new XMLHttpRequest();
//设置验证 参数 发送请求
if (request) {
method = method.toUpperCase();
if (method != "GET" && method != "POST") {
alert("HTTP的请求方法必须为GET或POST!!!");
return;
}
if (url == null || url == undefined) {
alert("HTTP的请求地址必须设置!");
return;
}
//服务器给XMLHttpRequest对象送回响应时触发
request.onreadystatechange = function() {
if (request.readyState == 0) {
//未初始化
} else if (request.readyState == 1) {
//正在加载
} else if (request.readyState == 2) {
//加载完毕
} else if (request.readyState == 3) {
//正在交互
} else if (request.readyState == 4) {
//完成
if (request.status == 200) {
//成功
console.log("服务器响应的文本内容-->" + request.responseText + "\n",
"服务器响应的XML内容对应的DOM对象-->" + request.responseXML + "\n",
"服务器返回状态的文本信息-->" + request.statusText);
callback && callback(request);
} else {
console.log("服务器响应的文本内容-->" + request.responseText + "\n",
"服务器响应的XML内容对应的DOM对象-->" + request.responseXML + "\n",
"服务器返回状态的文本信息-->" + request.statusText + "\n",
"状态码-->" + request.status);
failback && failback(request);
}
}
};
//解决缓存的转换
if (url.indexOf("?") >= 0) {
url = url + "&t=" + (new Date()).valueOf();
} else {
url = url + "?t=" + (new Date()).valueOf();
}
//解决跨域的问题
if (url.indexOf("http://") >= 0) {
url.replace("?", "&");
url = "Proxy?url=" + url;
}
//打开
request.open(method, url, true);
//如果是POST方式,需要设置请求头 要在send之前 open之后
if (method == "POST") {
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
}
//发送
request.send(data);
} else {
console.log("sorry, you browser dose not support XMLHttpRequest");
}
}
</script>
<script>
// 测试
document.getElementById("request").onclick = function() {
function callback(data) {
console.log(data);
}
function failback(data) {
console.log(data);
}
xmlHttp("get", "bsCarousel.js", {
test: 1
}, callback, failback);
}
</script>
</body>
</html>