-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
107 lines (96 loc) · 2.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
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
/**
* @include object2querystring, eachObject, apply
* @param {object} op
* @param {string} op.url
* @param {string} [op.method = 'get'] - [get|post]
* @param {string} [op.dataType = 'plain'] - [plain|json] - response data type
* @param {object} [op.data] - request data
* @param {string} [op.postType = 'text'] - [form|json] - request data type
* @param {object} [op.headers]
* @param {function} [op.beforeSend]
* @param {function} cb
* @return {object}
*/
function ajax(op, cb){
const xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
op = Object.assign({
xhr,
method: 'get',
dataType: 'plain',
postType: 'form',
headers: {},
}, op);
op.method = op.method.toLowerCase();
let postData;
// apply data
if(op.data){
if(op.method === 'get'){
op.url += object2querystring(op.data);
}else{
switch(op.postType){
case 'json':
postData = JSON.stringify(op.data);
op.headers['Content-Type'] = 'application/json';
break; case 'form': case null:
postData = object2querystring(op.data);
op.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
}
}
xhr.onreadystatechange = function(){
var rs = null, err = null;
if(xhr.readyState === 4){
if(xhr.status === 200){
rs = xhr.responseText;
}else{
err = 'ajax load error';
}
if(op.dataType === 'json'){
try{
rs = JSON.parse(rs);
}catch(err){
err = err;
}
}
apply(cb, [err, rs]);
}
};
xhr.open(op.method.toUpperCase(), op.url);
// apply headers
eachObject(op.headers, (value, key)=> xhr.setRequestHeader(key, value));
apply(op.beforeSend, [op]);
setTimeout(()=> xhr.send(postData));
return op;
}
ajax.get = function(url, op, cb){
if(isFunction(op)){
cb = op;
op = null;
}
return ajax(Object.assign({
url,
}, op), cb);
};
ajax.getJson = function(url, op, cb){
if(isFunction(op)){
cb = op;
op = null;
}
return ajax(Object.assign({
url,
dataType: 'json',
}, op), cb);
};
ajax.post = function(url, data, op, cb){
if(isFunction(op)){
cb = op;
op = null;
}
return ajax(Object.assign({
method: 'post',
dataType: 'json',
postType: 'form',
url,
data,
}, op), cb);
};