-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathajax.js
160 lines (144 loc) · 4.38 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
____________________________________________
| |
| sitepoint.com's Ajax Modified by ahmad_511 |
|____________________________________________|
*/
function Ajax(){
this.Method="GET";//String "GET" or "POST"
this.URL=null;// String
this.ResponseHandler=null;// Function
this.ErrorHandler=null;// Function
this.Request=null;
this.Async=true;// Boolean
this.Data = null;// String name=value pairs
this.ResponseFormat="text"; //String text, xml, request, object or json
this.ObjectState=null; //object can be used to send some info to the response handler function
this.init=function(){
if (!this.Request) {
try {
// Try to create object for Firefox, Safari, IE7, etc.
this.Request = new XMLHttpRequest();
}
catch (e) {
try {
// Try to create object for later versions of IE.
this.Request = new ActiveXObject('MSXML2.XMLHTTP');
}
catch (e) {
try {
// Try to create object for early versions of IE.
this.Request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
// Could not create an XMLHttpRequest object.
return false;
}
}
}
}
if(this.Request.overrideMimeType ){
if (this.ResponseFormat.toLowerCase()=="text")this.Request.overrideMimeType('text/html');
if (this.ResponseFormat.toLowerCase()=="xml")this.Request.overrideMimeType('text/xml');
}
return this.Request;
}
// converting an object to name=value pairs
this.decode=function(obj){
if(typeof obj=="object"){
var urlstr="";
for (var k in obj){
if(typeof obj[k]=="object"){// should be an array
for(var i=0;i<obj[k].length;i++){
urlstr+=k+"="+encodeURIComponent(obj[k][i])+"&";
}
}else{
urlstr+=k+"="+encodeURIComponent(obj[k])+"&";
}
}
if(urlstr.length>0)urlstr=urlstr.substr(0,urlstr.length-1);
}else urlstr=obj;
return urlstr;
}
// coverting name=value to pairsobject
this.encode=function(str){
if(typeof obj=="string"){
var obj={};
var objArr=str.split("&");
for (var i=0;i<objArr.length;i++){
var kv=objArr[i].split("=");
if (kv.length==2)obj[kv[0]]=decodeURIComponent(kv[1]);
}
}else obj=str;
return obj;
}
this.Send=function(){
if (!this.init()) {
alert('Could not create XMLHttpRequest object.');
return;
}
var self = this; // Fix loss-of-scope in inner function
self.ResponseFormat=self.ResponseFormat.toLowerCase();
this.Request.onreadystatechange=function() {
var resp = null;
if (self.Request.readyState == 4) {
switch (self.ResponseFormat.toLowerCase()) {
case "text":
resp = self.Request.responseText;
break;
case "xml":
resp = self.Request.responseXML;
break;
case "request":
resp = self.Request;
break;
case "object":
resp = self.encode(self.Request.responseText);
break;
case "json":
if(self.Request.responseText==""){
resp=null;
}else{
// check for Native JSON support (IE8+, FF3.5+, Safari 4+, Opera 10+, Chrome 4+), use eval if failed
if(window.JSON)
resp = JSON.parse(self.Request.responseText);
else resp = eval('('+self.Request.responseText+')');
}
break;
}
if (!self.Async){
if (self.ResponseHandler!=null)self.ResponseHandler(resp,self.ObjectState);
if(self.Request.onload)self.Request.onload=null;
}
if (self.Request.status >= 200 && self.Request.status <= 304) {
// when Async=false (none synchronized request)
// chrome triggers onLoad + onStateChange
// FF triggers onLoad, IE triggers onStateChange
if (self.ResponseHandler!=null)self.ResponseHandler(resp,self.ObjectState);
if(self.Request.onload)self.Request.onload=null;
}else {
if(self.ErrorHandler!=null)self.ErrorHandler(resp);
else alert("Problem while connecting to the server, Please refresh the page and try again");
}
}
};
if(this.Request.onload)this.Request.onload=this.Request.onreadystatechange
if (this.Method.toLowerCase()=="get"){
this.URL +="?"+this.decode(this.Data)
this.Data=null
}else{
this.Data=this.decode(this.Data)
}
this.Request.open(this.Method, this.URL, this.Async);
if (this.Method.toLowerCase()=="post")this.Request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
this.Request.send(this.Data);
};
// abort
this.abort = function() {
if (this.Request) {
this.Request.onreadystatechange = function() {};
this.Request.abort();
this.Request = null;
};
};
};