AJAXRequest v0.2

Source: Internet
Author: User

Update:

1) Change the constructor to include parameters to simplify the procedure

Class Name: AJAXRequest

Creation method:

Var ajaxobj = new AJAXRequest (method, url, async, content, callback );

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">
// Request Method GET, URL is default. asp, asynchronous
Var ajaxobj = new AJAXRequest ("GET", "default. asp", true, null, MyCallback); // create an AJAX object
Ajaxobj. send (); // send the request
Function MyCallback (xmlObj ){
Document. write (xmlobj. responseText );
}

Ajaxrequest. js
Copy codeThe Code is as follows:
/*------------------------------------------
Author: xujiwei
Web: 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 );
}
}

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.