Start of front-end development-object-oriented Ajax class

Source: Internet
Author: User

First look at the call method:

CopyCode The Code is as follows: Ajax. Request ("ajax.html", {v: Math. Random (), num: 1}, function (data ){
// Do something
}, 'Get ');

The method is like jquery... I still think this call is more convenient...Copy codeThe Code is as follows: var Ajax = {
// XMLHttpRequest class
XMLHttpRequest: function (){
This. xhr = false;
},
// External call interface
Request: function (URL, Data, callback, type ){
// Create an XMLHTTPRequest object each time, so that AJAX calls do not affect each other
VaR xhr = new this. XMLHttpRequest ();
Xhr. Request (URL, Data, callback, type );
}
}
// Convert the JSON data format {num: 1, t: 'A'} to the string format num = 1 & t =.
VaR json2str = function (data ){
VaR _ DATA = [];

For (VAR name in data ){
_ Data. Push (name + "=" + data [name]);
}
Return _ data. Join ('&');
}
// Method for extending the XMLHttpRequest class
Ajax. XMLHttpRequest. Prototype = {
// Create XMLHttpRequest
Createxmlhttprequest: function (){

If (window. XMLHttpRequest ){
Return new XMLHttpRequest ();
}
Else {
VaR A = ["msxml2.xmlhttp", "Microsoft. XMLHTTP", "msxml2.xmlhttp. 5.0", "msxml2.xmlhttp. 4.0", "msxml2.xmlhttp. 3.0"];
For (VAR I = 0, L = A. length; I <L; I ++ ){
Try {
Return new activexobject (A [I]);
} Catch (e ){};
}
}
},
// Callback function
Fncallback: function (callback ){

If (this. xhr. readystate ===4 & this. xhr. Status === 200 ){
Callback? Callback (this. xhr. responsetext): void (0 );
}
},
// Ajax request
Request: function (URL, Data, callback, type ){

VaR that = this;
VaR ispost = type = 'post '? True: false;

Ispost? URL: URL + = '? '+ Json2str (data );

This. xhr = This. createxmlhttprequest ();

This. xhr. Open (type, URL, true );
Ispost? This. xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded "):'';
This. xhr. onreadystatechange = function () {That. fncallback (callback );};
This. xhr. Send (ispost? Json2str (data): NULL );
}
}

There must be some shortcomings in this category. Please click it! Everyone has their own habits. The most important thing is that you can use them!

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.