-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
44 lines (44 loc) · 1.18 KB
/
ajax.js
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
function ajax(options){
let xhr;
if (window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}else{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange=function(){
if (xhr.readyState==4 && xhr.status==200){
options.success(this.responseText);
}else if(xhr.readyState==4){
if(options.fail){
options.fail(this.statusText);
}else{
console.log(this.statusText);
}
}
}
xhr.open(options.method||"GET",options.url,true);
if(options.beforeSend){
options.beforeSend(xhr);
}
if(options.send){
if(options.sendtype==="fd"){
let formdata=new FormData();
for(let [key,value] of options.send){
formdata.appear(key,value);
}
xhr.send(formdata);
}else if(options.sendtype==="fd2"){
xhr.send(new FormData(options.send));
}else if(options.sendtype==="send"){
xhr.send(options.send);
}else if(options.sendtype==="json"){
xhr.send(JSON.stringify(options.send));
}else{
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
xhr.send(options.send);
}
}else{
xhr.send();
}
return xhr;
}