How to change the constructor AJAXRequest function to simplify the use of parameters
This article introduces the knowledge of "how to change the construction of AJAXRequest functions to simplify the use of parameters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Update:
1) change the constructor to make it easier to use with parameters
Class name: AJAXRequest
Creation method:
Var ajaxobj=new AJAXRequest (method,url,async,content,callback)
Return 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:
The code is as follows:
/ / request method GET,URL is default.asp, asynchronous
Var ajaxobj=new AJAXRequest ("GET", "default.asp", true,null,MyCallback); / / create AJAX object
Ajaxobj.send (); / / send the request
Function MyCallback (xmlObj) {
[xss_clean] (xmlobj.responseText)
}
Ajaxrequest.js
The code is as follows:
/ *--
Author: xujiwei
Website: http://www.xujiwei.cn
E-mail: vipxjw@163.com
Copyright (c) 2006, All Rights Reserved
-- * /
Function AJAXRequest (pmethod,purl,pasync,pcontent,pcallback) {
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=pmethod
This.url=purl
This.async=pasync
This.content=pcontent
This.callback=pcallback
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)
}
}
This is the end of "how to change the construction AJAXRequest function to simplify the use of parameters". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!