-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxMethod.js
More file actions
39 lines (35 loc) · 1.04 KB
/
Copy pathAjaxMethod.js
File metadata and controls
39 lines (35 loc) · 1.04 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
function Ajax(method, adress, params, headers, respFunc){
if ((method !== "GET")
&& (method !== "POST")
&& (method !== "DELETE")){
method = "GET";
}
var xhr = new XMLHttpRequest();
var paramsString = "";
if ( {}.toString.call(params) === '[object Object]' ) {
var num = 0;
for(var key in params) {
if (num === 0){
paramsString = "?";
}else{
paramsString += "&";
};
paramsString += key + "=" + params[key];
num++;
};
};
xhr.open(method, adress + paramsString, true);
if (typeof respFunc === "function") {
xhr.onreadystatechange = function() {
if (this.readyState !== 4) return;
respFunc(xhr.responseText);
};
};
if ( {}.toString.call(headers) === '[object Object]' ) {
for(var key in headers) {
xhr.setRequestHeader(key, headers[key]);
};
};
xhr.send("");
return xhr;
};