Usage of AJAX request class
This article mainly explains "the usage of AJAX request class". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of AJAX request class".
The code is as follows:
/ / AJAX class
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
This.method= "POST"
This.url
This.async=true
This.content= ""
This.callback=function (cbobj) {return;}
This.send=function () {
If (! this.method | |! this.url | |! this.async) return false
XmlObj.open (this.method, this.url, this.async)
If (this.method== "POST") xmlObj.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")
XmlObj.onreadystatechange=function () {
If (xmlObj.readyState==4) {
If (xmlObj.status==200) {
ObjSelf.callback (xmlObj)
}
}
}
If (this.method== "POST") xmlObj.send (this.content)
Else xmlObj.send (null)
}
}
AJAX request class
By HotHeart (warm-hearted)
Site: http://www.xujiwei.cn/
Blog: http://www.xujiwei.cn/blog/
Class name: AJAX
Creation method: var ajaxobj=new AJAX;, returns false if creation fails
Attribute: method-request method, string, POST or GET. Default is POST
Url-request URL, string, empty by default
Async-whether asynchronous. True is asynchronous, false is synchronous. Default is true.
Content-the content of the request. If the request method is POST, this property needs to be set. Default is empty.
Callback-callback function, that is, the function that is called when the response content is returned. By default, the callback function is returned directly. The callback function has a parameter of XMLHttpRequest object, that is, the callback function should be defined as follows: function mycallback (xmlobj)
Method: send ()-send a request with no parameters
An example:
Var ajaxobj=new AJAXRequest; / / create AJAX object
Ajaxobj.method= "GET"; / / set the request method to GET
Ajaxobj.url= "default.asp" / / URL is default.asp
/ / set the callback function and output the response content
Ajaxobj.callback=function (xmlobj) {
[xss_clean] (xmlobj.responseText)
}
Ajaxobj.send (); / / send the request
Thank you for reading, the above is the content of "the usage of AJAX request class". After the study of this article, I believe you have a deeper understanding of the usage of AJAX request class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!