A simple AJAX request class

Source: Internet
Author: User

After I added the refresh search and instant verification detection to my blog, I read the code again. I felt too much trouble. I encapsulated the XMLHttpRequest request into a class, which is much more convenient to use, there is no need to remember so much code, what to create the XMLHttpRequest object or something, and this part of code is also highly reusable ~ It has been packaged and downloaded at the end of the log.

If you want to see the effect, click the log search in the sidebar. There is a refreshing search in it, or there is instant detection in the registration code in the reading log or message book, if you do not enter the verification code or enter the wrong verification code, the input box will become red ^_^

Class Name: AJAXRequest

Creation Method: var ajaxobj = new AJAXRequest; if creation fails, false is returned.

Attribute: method-Request method, String, POST, or GET. The default value is POST.

Url-request URL, String, empty by default

Async-asynchronous or not. true indicates asynchronous, and false indicates synchronous. The default value is true.

Content-the content of the request. If the request method is POST, this attribute must be set. The default value is null.

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)

Method: send-send request, no Parameter

Example:Copy codeThe Code is as follows: <script type = "text/javascript" src = "ajaxrequest. js"> </script>
<Script type = "text/javascript">
Var ajaxobj = new AJAXRequest; // create an AJAX object
Ajaxobj. method = "GET"; // set the request method to GET
Ajaxobj. url = "default. asp" // The URL is default. asp.
// Set the callback function to output the response content
Ajaxobj. callback = function (xmlobj ){
Document. write (xmlobj. responseText );
}
Ajaxobj. send (); // send the request

Copy codeThe 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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.