-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxhr.js
More file actions
39 lines (37 loc) · 1.2 KB
/
xhr.js
File metadata and controls
39 lines (37 loc) · 1.2 KB
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
/**
* ajax POST json数据
* @param url
* @param data
* @param callback
*/
function postJSON(url, data, callback) {
var request = new XMLHttpRequest();
request.open('POST', url);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
callback(request.responseText)
}
}
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify(data));
}
function creatXhr() {
if (typeof XMLHttpRequest != 'undefined') {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != 'undefined') {
if(typeof arguments.callee.activeXString!='string'){
var versions=['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];
for(var i=0;i<versions.length;i++){
try{
var xhr=new ActiveXObject(versions[i]);
arguments.callee.activeXString=versions[i];
return xhr;
}catch(ex){
}
}
return new ActiveXObject(arguments.callee.activeXString);
}
} else {
throw new Error('XMLHttpRequest is not supported!');
}
}