-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroajax.js
44 lines (36 loc) · 1.05 KB
/
microajax.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
function microAjax(url, callbackFunction)
{
this.bindFunction = function (caller, object) {
return function() {
return caller.apply(object, [object]);
};
};
this.stateChange = function (object) {
if (this.request.readyState==4)
this.callbackFunction(this.request.responseText);
};
this.getRequest = function() {
if (window.ActiveXObject)
return new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
return false;
};
this.postBody = (arguments[2] || "");
this.callbackFunction=callbackFunction;
this.url=url;
this.request = this.getRequest();
if(this.request) {
var req = this.request;
req.onreadystatechange = this.bindFunction(this.stateChange, this);
if (this.postBody!=="") {
req.open("POST", url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader('Connection', 'close');
} else {
req.open("GET", url, true);
}
req.send(this.postBody);
}
}