Ajax request class

Source: Internet
Author: User

CopyCode 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)
Site: http://www.xujiwei.cn/
Blog: http://www.xujiwei.cn/blog/

Class Name: Ajax

Creation Method: var ajaxobj = new Ajax;. 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:
<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
</SCRIPT>

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.