Js lite Ajax Functions

Source: Internet
Author: User

Version: version 1.0

Author: lz_wu

Time:

Call method:

AjaxHandler. Ajax ({

Url: "webtest. aspx ",

Type: "get ",

Data: {name: "joy "},

DataType: "json ",

Success: function (result ){

Alert (result. name );

},

Error: function (e, status ){

Alert (e. responseText );

// Var result = eval ("(" + E. responsetext + ")");

// Alert (E. Exception. Message );

}

});

Wish: I hope someone can improve this function even more after seeing and using it. Thank you for your attention.

The Code is as follows:

VaR ajaxhandler = {

Defaultoptions :{

URL: "", // URL

Type: "Get", // RESPONSE METHOD

Async: True, // synchronous or skip

Timeout:-1, // supertime settings

Cache: True, // reserved storage settings

Data: {}, // numeric value

Datatype: "", // return type

Success: function () {}, // number of rows written when the operation succeeds

Error: function () {}// number of response rows when the error occurs

},

TiemOutSet: null, // superhour

XmlHttp: function () {// construct the XMLHttpRequest object

Var xmlhtp = null;

If (window. XMLHttpRequest)

Xmlhtp = new XMLHttpRequest ();

Else {

If (window. ActiveXObject ){

Xmlhtp = new ActiveXObject ("Microsoft. XMLHTTP") | new ActiveXObject ("Msxml2.XMLHTTP ");

}

}

Return xmlhtp;

},

Extend: function (arg1, arg2) {// reset the default settings

For (var n in arg2 ){

If (arg1 [n]! = Undefined)

Arg2 [n] = arg1 [n];

}

},

Ajax: function (obj ){

Var xmlhttpRequest = this. xmlHttp ();

If (xmlhttpRequest = null) {alert ("you bowerse is not support Ajax! "); Return ;}

This. extend (obj, this. defaultOptions );

Var current = this;

XmlhttpRequest. onreadystatechange = function (){

Current. AjaxSuccess (xmlhttpRequest );

}

XmlhttpRequest. open (this. defaultOptions. type, this. getAjaxUrl (), this. defaultOptions. async );

This. setHeaderInfo (xmlhttpRequest); // sets the header information.

XmlhttpRequest. send (null); // send request

This. timeoutHandler (this. stopTimeOut, xmlhttpRequest); // timeout handling

},

SetHeaderInfo: function (xmlhttpRequest) {// specifies the information of the preset header.

If (this. defaultOptions. dataType = "" & this. defaultOptions. type. toLowerCase () = "post "){

XmlhttpRequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8 ");

}

If (this. defaultOptions. dataType! = ""){

Switch (this. defaultOptions. dataType. toLowerCase () // return type

{

Case "xml": xmlhttpRequest. setRequestHeader ("Content-Type", "text/xml; charset = UTF-8"); break;

Case "html": xmlhttpRequest. setRequestHeader ("Content-Type", "text/html; charset = UTF-8"); break;

Case "json": break;

Case "script": break;

Default: xmlhttpRequest. setRequestHeader ("Content-Type", "text/xml; charset = UTF-8 ");

Break;

}

}

},

GetAjaxUrl: function () {// url Processing

Var url = "";

Var arr = new Array ();

For (var name in this. defaultOptions. data)

Arr. push (name + "=" + this. defaultOptions. data [name]);

If (arr. length! = 0) url = this. defaultOptions. url + "? "+ Arr. join ("&");

Else url = this. defaultOptions. url;

If (this. defaultOptions. cache = false) {// indicates no memory is saved.

If (url. indexOf ("? ") =-1)

Url + = "? TimeSpan = "+ new Date (). getTime ();

Else

Url + = "& timeSpan =" + new Date (). getTime ();

}

Return url;

},

TimeoutHandler: function (callback, xmlhttpRequest ){

If (this. defaultOptions. timeout! =-1 ){

// Var _ cb = function (){}

This. tiemOutSet = setTimeout (function (){

Callback. apply (null, xmlhttpRequest );

}, This. defaultOptions. timeout );

}

},

Stoptimeout: function (XMLHttpRequest) {// indicates the number of processed functions in the superhour.

If (this. tiemoutset! = NULL ){

If (XMLHttpRequest) XMLHttpRequest. Abort (); // prevents submission

Cleartimeout (this. tiemoutset); // clear the supertime

This. tiemoutset = NULL;

}

},

Ajaxsuccess: function (XMLHttpRequest) {// number of response rows when the call succeeds

If (XMLHttpRequest ){

Switch (XMLHttpRequest. readystate ){

Case 0: break; // you have created an XMLHTTPRequest object, but it has not been initialized yet.

Case 1: break; // the XMLHttpRequest open () method has been called and XMLHttpRequest is ready to send a request to the server.

Case 2: break; // a request has been sent to the server through the send () method, but no response has been received.

Case 3: break; // The HTTP Response Header has been received, but the message body has not completely received.

Case 4: // "status loaded". At this time, the response has been fully received.

If (xmlhttpRequest. status = 200) {// indicates successful

This. defaultOptions. success (this. returnType (xmlhttpRequest ));

}

Else

This. defaultOptions. error (xmlhttpRequest, xmlhttpRequest. status );

Break

Default: break;

}

 

}

},

ReturnType: function (xmlhttpRequest) {// return the processing of the type

Var content;

If (this. defaultOptions. dataType! = ""){

Switch (this. defaultOptions. dataType. toLowerCase ()){

Case "xml": content = xmlhttpRequest. responseXML; break;

Case "html": content = xmlhttpRequest. responseText; break;

Case "json": content = eval ('+ xmlhttpRequest. responseText +'); break;

Default: content = xmlhttpRequest. responseText; break;

}

}

Else

Content = xmlhttpRequest. responseText;

Return content;

}

};

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.