A simple Ajax request class _ajax related

Source: Internet
Author: User
In addition to the blog without refreshing search and instant verification detection, and looked at the code, feel too troublesome, the XMLHttpRequest request package into a class inside, use it more convenient, do not remember so much code, what create XMLHttpRequest object or something, This part of the code is also relatively high reusability ~ has been packaged, at the end of the log download.

To see the effect of the point open the side of the log search, there is a no refresh search, it is, or in the reading log or in the book in the registration code where there is real-time detection, if you do not enter the verification code or the wrong authentication code, the input box will turn red ^_^

Class Name: Ajaxrequest

Create method: Var ajaxobj=new ajaxrequest, return False if creation fails

Properties: Method-Request methods, strings, post or get, default to post

URL-Request URL, string, default is NULL

Async-Asynchronous, true to asynchronous, false to synchronous, default to True

Content-the contents of the request, if this attribute is set by the request method for post, the default is null

Callback-callback function, that is, the function called when the response content is returned, the default is direct return, and the callback function has a parameter of XMLHttpRequest object, that is, when defining a callback function: function Mycallback (xmlobj)

Method: send-sending request, no parameters

An example:
Copy Code code as follows:

<script type= "Text/javascript" src= "Ajaxrequest.js" ></script>
<script type= "Text/javascript" >
var ajaxobj=new ajaxrequest; Creating Ajax Objects
Ajaxobj.method= "Get"; Set the request mode to get
Ajaxobj.url= "Default.asp"//URL is default.asp
Set callback function, output response content
Ajaxobj.callback=function (xmlobj) {
document.write (Xmlobj.responsetext);
}
Ajaxobj.send (); Send Request

Copy Code code as follows:

Ajax classes
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.