Name: AJAXRequest
Author: HotHeart (xujiwei)
Site: http://www.xujiwei.cn/
Blog: http://www.xujiwei.cn/blog/
Copyright (c) 2006, All Rights Reserved
Class Name: AJAXRequest
Version: 0.3
Date: 2006-12-18
Description: AJAXRequest is a common class for convenient AJAX development. It can be used to conveniently perform operations required in AJAX, so as to simplify the development steps and reduce the amount of repeated code writing.
Creation method:
Var ajaxobj = new AJAXRequest ([url], [callback], [content], [method], [async]);
If creation fails, false is returned.
Attribute:
Url-request URL, String, empty by default
The callback-callback function is the function called when the response content is returned. The default value is direct return. The callback function has a parameter XMLHttpRequest object, that is, the callback function must be defined as follows: function mycallback (xmlobj)
Content-the content of the request. If the request method is POST, this attribute must be set. The default value is a null string.
Method-Request method, String, POST, or GET. The default value is POST.
Async-asynchronous or not. true indicates asynchronous, and false indicates synchronous. The default value is true.
Method
Function send ([url], [callback], [content], [method], [async])
Send a request. If the list of optional parameters is empty, the object attribute is used.
Function get ([url], [callback])
The GET method is used to request a URL. The optional parameters use object attributes by default.
Function post (form_obj, [callback], [url], [method])
Send a form to the specified URL. form_obj is the specified form object. If the optional parameter is null, the object attribute is used.
Example:
1. get Method
Function test1 (){
Var ajax = new AJAXRequest;
Ajax. get (
"Test. asp ",
Function (obj ){
Document. getElementById ("test1"). value = obj. responseText;
}
);
}
2. post Method
Function test2 (){
Var ajax = new AJAXRequest;
Ajax. post (
Document. getElementById ("test2c "),
Function (obj ){
Document. getElementById ("test2r"). innerHTML = obj. responseText;
}
);
}
Copy codeThe Code is as follows:
/*------------------------------------------
Author: xujiwei
Web: http://www.xujiwei.cn
E-mail: vipxjw@163.com
Copyright (c) 2006, All Rights Reserved
------------------------------------------*/
Function AJAXRequest (){
Var xmlObj = false;
Var CBfunc, ObjSelf;
ObjSelf = this;
Try {xmlObj = new XMLHttpRequest ;}
Catch (e ){
Try {xmlObj = new ActiveXObject ("MSXML2.XMLHTTP ");}
Catch (e2 ){
Try {xmlObj = new ActiveXObject ("Microsoft. XMLHTTP ");}
Catch (e3) {xmlObj = false ;}
}
}
If (! XmlObj) return false;
If (arguments [0]) this. url = arguments [0]; else this. url = "";
If (arguments [1]) this. callback = arguments [1]; else this. callback = function (obj) {return };
If (arguments [2]) this. content = arguments [2]; else this. content = "";
If (arguments [3]) this. method = arguments [3]; else this. method = "POST ";
If (arguments [4]) this. async = arguments [4]; else this. async = true;
This. send = function (){
Var purl, pcbf, pc, pm, pa;
If (arguments [0]) purl = arguments [0]; else purl = this. url;
If (arguments [1]) pc = arguments [1]; else pc = this. content;
If (arguments [2]) pcbf = arguments [2]; else pcbf = this. callback;
If (arguments [3]) pm = arguments [3]; else pm = this. method;
If (arguments [4]) pa = arguments [4]; else pa = this. async;
If (! Pm |! Purl |! Pa) return false;
XmlObj. open (pm, purl, pa );
If (pm = "POST") xmlObj. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XmlObj. onreadystatechange = function (){
If (xmlObj. readyState = 4 ){
If (xmlObj. status = 200 ){
Pcbf (xmlObj );
}
Else {
Pcbf (null );
}
}
}
If (pm = "POST ")
XmlObj. send (pc );
Else
XmlObj. send ("");
}
This. get = function (){
Var purl, pcbf;
If (arguments [0]) purl = arguments [0]; else purl = this. url;
If (arguments [1]) pcbf = arguments [1]; else pcbf = this. callback;
If (! Purl &&! Pcbf) return false;
This. send (purl, "", pcbf, "GET", true );
}
This. post = function (){
Var fo, pcbf, purl, pc, pm;
If (arguments [0]) fo = arguments [0]; else return false;
If (arguments [1]) pcbf = arguments [1]; else pcbf = this. callback;
If (arguments [2])
Purl = arguments [2];
Else if (fo. action)
Purl = fo. action;
Else
Purl = this. url;
If (arguments [3])
Pm = arguments [3];
Else if (fo. method)
Pm = fo. method. toLowerCase ();
Else
Pm = "post ";
If (! Pcbf &&! Purl) return false;
Pc = this. formToStr (fo );
If (! Pc) return false;
If (pm ){
If (pm = "post ")
This. send (purl, pc, pcbf, "POST", true );
Else
If (purl. indexOf ("? ")> 0)
This. send (purl + "&" + pc, "", pcbf, "GET", true );
Else
This. send (purl + "? "+ Pc," ", pcbf," GET ", true );
}
Else
This. send (purl, pc, pcbf, "POST", true );
}
// FormToStr
// From SurfChen <surfchen@gmail.com>
// @ Url http://www.surfchen.org/
// @ License http://www.gnu.org/licenses/gpl.html GPL
// Modified by xujiwei
// @ Url http://www.xujiwei.cn/
This. formToStr = function (fc ){
Var I, query_string = "", and = "";
For (I = 0; I <fc. length; I ++ ){
E = fc [I];
If (e. name! = ''){
If (e. type = 'select-one '){
Element_value = e. options [e. selectedIndex]. value;
}
Else if (e. type = 'checkbox' | e. type = 'Radio '){
If (e. checked = false ){
Continue;
}
Element_value = e. value;
}
Else {
Element_value = e. value;
}
Element_value = encodeURIComponent (element_value );
Query_string + = and + e. name + '=' + element_value;
And = "&";
}
}
Return query_string;
}
}