forked from yueyingjun/2.16
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.js
More file actions
60 lines (56 loc) · 1.5 KB
/
Copy pathajax.js
File metadata and controls
60 lines (56 loc) · 1.5 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function ajax(obj) {
if(typeof obj!=="object"){
console.error("请输入正确的参数");
return;
}
if (typeof obj.url!=="string") {
console.error("请输入正确的地址");
return;
}
var url=obj.url;
var data=obj.data||"";
if(typeof data=="object"){
var str="";
for(var i in data){
str+=i+"="+data[i]+"&";
}
data=str.slice(0,-1);
}
var type=obj.type||"get";
var bool=obj.bool===undefined?true:obj.bool;
var datatype=obj.dataType||"text";
var succ=obj.success;
var error=obj.error;
var xml=new XMLHttpRequest();
xml.onreadystatechange=function(){
if(xml.readyState==4){
if (xml.status==200){
var result;
if(datatype==="xml"){
result=xml.responseXML;
}else{
result=xml.response;
}
succ(result)
}else if(xml.status==404){
var info="页面未找到";
error(info);
}
}
}
if (type=="get"){
xml.open(type,url+"?"+data,bool);
if(datatype!="xml") {
xml.responseType = datatype;
}
xml.send();
}
if (type=="post"){
xml.open(type,url,bool);
xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(datatype!="xml") {
xml.responseType = datatype;
}
xml.send(data)
}
}