-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
51 lines (45 loc) · 1.43 KB
/
request.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
function Request(handlers){
//event handler functions
this.onData = handlers.onData;
this.onError = handlers.onError ? handlers.onError : false;
this.onLoading = handlers.onLoading ? handlers.onLoading : false;
this.lastRequest = "";
var xmlhttp=false;
var This = this;
//create xmlhttp object.. http://jibbering.com/2002/4/httprequest.htm
try { //ie
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { //moz
xmlhttp = new XMLHttpRequest();
}
this.stateChange = function( ){
if ( xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var response = (xmlhttp.responseText) ? xmlhttp.responseText : "";
This.onData(response, This);
} else {
var e = xmlhttp.statusText;
if(This.onError) This.onError(e, This);
}
}
}
this.send = function( url, requestHeaders ) {
if(xmlhttp){
if(This.onLoading) This.onLoading();
xmlhttp.open("POST", url , true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = This.stateChange;
This.lastRequest = url;
xmlhttp.send(null);
}else{
if(This.onError) This.onError("Sorry, your browser does not support remote requests.");
}
}
}