JavaScript AJAX类
function AjaxTool(){};
3 AjaxTool.prototype=
4 {
5 ajaxPool:[], //XMLHttpRequest 连接池
6 userName:'', //用户访问帐号
7 userPwd:'', //用户访问密码
8 requestHeaders:{'Count':0,'Items':[]}, //存放请求头
9 getInstance:function()
10 {
11 for(var i=0;i<this.ajaxPool.length;i++)
12 {
13 if(this.ajaxPool[i].readyState==0 || this.ajaxPool[i].readyState==4)
14 return this.ajaxPool[i];
15 }
16 this.ajaxPool[length]=this.createXmlHttp();
17 return this.ajaxPool[length];
18 }, //end of getInstance();
19 createXmlHttp:function()
20 {
21 if(window.XMLHttpRequest)
22 {
23 return new XMLHttpRequest();
24 }
25 else
26 {
27 var xmlHttps=['Microsoft.XMLHTTP','MSXML2.XMLHTTP','MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0'];
28 for(var i=0;i<xmlHttps.length;i++)
29 {
30 try
31 {
32 return new ActiveXObject(xmlHttps[i]);
33 }
34 catch ($e)
35 {
36 continue;
37 }
38 }
39 }
40 }, //end of createXmlHttp()
41 setLoginInfo:function(username,password)
42 {
43 if(typeof username!=='string' || username.length<=0)
44 throw new Error('UserName must be a string');
45 if(typeof password!=='string' || password.length<=0)
46 throw new Error('Password must be a string');
47 this.userName=username;
48 this.userPwd=password;
49 }, //end of setLoginInfo
50 setRequestHeader:function (name, value)
51 {
52 if ('string' !== typeof(name) || name.length<=0)
53 {
54 throw new Error('RequestHeader\'s name must be a string!');
55 }
56 if ('string' !== typeof(value)|| value.length<=0)
57 {
58 throw new Error('RequestHeader\'s value must be a string!');
59 }
60 this.requestHeaders.Count = this.requestHeaders.Count + 1;
61 this.requestHeaders.Items[name] = value;
62 }, //end of setRequestHeader()
63 handleCallBack:function(xmlHtp,callBack,args)
64 {
66 return function()
67 {
68 if(xmlHtp.readyState==4)
69 {
70 if(xmlHtp.status==200)
71 {
72 if(args.length>5)
73 {
74 var extendArgs=[];
75 for(var i=5;i<args.length;i++)
76 {
77 extendArgs.push(args[i]);
78 callBack(xmlHtp,extendArgs);
79 }
80 }
81 else
82 {
83 callBack(xmlHtp);
84 }
85 }
86 }
87 }
88 }, //end of handlerCallBack
89 sendRequest:function(method,url,data,callBack,disableCache)
90 {
91 if (typeof(method)!=='string')
92 {
93 throw new Error('method must be a string type');
94 }
95 if (typeof(url)!=='string')
96 {
97 throw new Error('url must be a string type');
98 }
99 method = method.toLowerCase();
100 if ('get' !== method && 'post' !== method)
101 {
102 method = 'get';
103 }
104 var async = ('function' === typeof(callBack));
105 try
106 {
107 var xmlHtp=this.getInstance();
108 if(async)
109 {
110 xmlHtp.onreadystatechange=this.handleCallBack(xmlHtp,callBack,arguments);
111 if(method=='get' && typeof(data)=='string' && data.length>0)
112 {
113 xmlHtp.open(method,url+(url.indexOf('?')!=-1?('&'+data):('?'+data)),async,this.userName,this.userPwd);
114 }
115 else
116 {
117 xmlHtp.open(method,url,async,this.userName,this.userPwd);
118 }
119 if (true === disableCache)
120 {
121 this.setRequestHeader('If-Modified-Since', '0');
122 }
123 if (this.requestHeaders.Count > 0)
124 {
125 for (var key in this.requestHeaders.Items)
126 {
127 xmlHtp.setRequestHeader(key, this.requestHeaders.Items[key]);
128 }
129 }
130 if ('get' === method)
131 {
132 xmlHtp.send(null);
133 }
134 else
135 {
136 xmlHtp.send(data);
137 }
138 }
139 else
140 {
141 return xmlHtp;
142 }
143 }
144 catch ($e)
145 {
146 throw $e;
147 }
148 } //end of sendRequest([args]);
149 };
3 AjaxTool.prototype=
4 {
5 ajaxPool:[], //XMLHttpRequest 连接池
6 userName:'', //用户访问帐号
7 userPwd:'', //用户访问密码
8 requestHeaders:{'Count':0,'Items':[]}, //存放请求头
9 getInstance:function()
10 {
11 for(var i=0;i<this.ajaxPool.length;i++)
12 {
13 if(this.ajaxPool[i].readyState==0 || this.ajaxPool[i].readyState==4)
14 return this.ajaxPool[i];
15 }
16 this.ajaxPool[length]=this.createXmlHttp();
17 return this.ajaxPool[length];
18 }, //end of getInstance();
19 createXmlHttp:function()
20 {
21 if(window.XMLHttpRequest)
22 {
23 return new XMLHttpRequest();
24 }
25 else
26 {
27 var xmlHttps=['Microsoft.XMLHTTP','MSXML2.XMLHTTP','MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0'];
28 for(var i=0;i<xmlHttps.length;i++)
29 {
30 try
31 {
32 return new ActiveXObject(xmlHttps[i]);
33 }
34 catch ($e)
35 {
36 continue;
37 }
38 }
39 }
40 }, //end of createXmlHttp()
41 setLoginInfo:function(username,password)
42 {
43 if(typeof username!=='string' || username.length<=0)
44 throw new Error('UserName must be a string');
45 if(typeof password!=='string' || password.length<=0)
46 throw new Error('Password must be a string');
47 this.userName=username;
48 this.userPwd=password;
49 }, //end of setLoginInfo
50 setRequestHeader:function (name, value)
51 {
52 if ('string' !== typeof(name) || name.length<=0)
53 {
54 throw new Error('RequestHeader\'s name must be a string!');
55 }
56 if ('string' !== typeof(value)|| value.length<=0)
57 {
58 throw new Error('RequestHeader\'s value must be a string!');
59 }
60 this.requestHeaders.Count = this.requestHeaders.Count + 1;
61 this.requestHeaders.Items[name] = value;
62 }, //end of setRequestHeader()
63 handleCallBack:function(xmlHtp,callBack,args)
64 {
66 return function()
67 {
68 if(xmlHtp.readyState==4)
69 {
70 if(xmlHtp.status==200)
71 {
72 if(args.length>5)
73 {
74 var extendArgs=[];
75 for(var i=5;i<args.length;i++)
76 {
77 extendArgs.push(args[i]);
78 callBack(xmlHtp,extendArgs);
79 }
80 }
81 else
82 {
83 callBack(xmlHtp);
84 }
85 }
86 }
87 }
88 }, //end of handlerCallBack
89 sendRequest:function(method,url,data,callBack,disableCache)
90 {
91 if (typeof(method)!=='string')
92 {
93 throw new Error('method must be a string type');
94 }
95 if (typeof(url)!=='string')
96 {
97 throw new Error('url must be a string type');
98 }
99 method = method.toLowerCase();
100 if ('get' !== method && 'post' !== method)
101 {
102 method = 'get';
103 }
104 var async = ('function' === typeof(callBack));
105 try
106 {
107 var xmlHtp=this.getInstance();
108 if(async)
109 {
110 xmlHtp.onreadystatechange=this.handleCallBack(xmlHtp,callBack,arguments);
111 if(method=='get' && typeof(data)=='string' && data.length>0)
112 {
113 xmlHtp.open(method,url+(url.indexOf('?')!=-1?('&'+data):('?'+data)),async,this.userName,this.userPwd);
114 }
115 else
116 {
117 xmlHtp.open(method,url,async,this.userName,this.userPwd);
118 }
119 if (true === disableCache)
120 {
121 this.setRequestHeader('If-Modified-Since', '0');
122 }
123 if (this.requestHeaders.Count > 0)
124 {
125 for (var key in this.requestHeaders.Items)
126 {
127 xmlHtp.setRequestHeader(key, this.requestHeaders.Items[key]);
128 }
129 }
130 if ('get' === method)
131 {
132 xmlHtp.send(null);
133 }
134 else
135 {
136 xmlHtp.send(data);
137 }
138 }
139 else
140 {
141 return xmlHtp;
142 }
143 }
144 catch ($e)
145 {
146 throw $e;
147 }
148 } //end of sendRequest([args]);
149 };
<h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>