my ajax class

 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
function Ajax()
{
this.requestObj = null;
this.construct = function ()
{
if (window.XMLHttpRequest)
{
return new XMLHttpRequest;
}
else if (window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else return null;
}
this.getReadyStateHandler = function (req, handler, params)
{
return function ()
{
handler(req, params);
}
}
this.sendRequest = function (url, params, method, myFunc)
{
this.requestObj = this.construct();
if (!this.requestObj) return false;
if (!method || !method.length || method.length < 3) method = "POST";
this.requestObj.onreadystatechange = this.getReadyStateHandler(this.requestObj, myFunc, this.parseParams(params));
this.requestObj.open(method,url,true);
this.requestObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.requestObj.send(params);
}
this.parseParams = function(str)
{
var vals = str.split('&');
if(!vals.length || (vals.length == 1 && vals[0] == '')) return str;
var pers = new Array;
var mas = new Object;
var newstr = '({';
for (var i = 0; i < vals.length; i++)
{
pers = vals[i].split('=');
newstr += pers[0]+':"'+ pers[1]+'"';
if(vals[i+1]) newstr += ',';
}
newstr += '})';
return eval(newstr);
}
this.getText = function(reg)
{
return reg.responseText;
}
this.getXML = function(reg)
{
return reg.responseXML;
}
this.getReady = function(req)
{
if (req.readyState == 4)
{
return true;
}
}
this.getJson = function(req)
{
return eval(eval("("+this.getText(req)+")"));
}
this.construct();
}