The beginning of front-end development---object-oriented AJAX class _js Object oriented

Source: Internet
Author: User
First look at the calling mode:

Copy Code code as follows:

Ajax.request ("ajax.html", {v:math.random (), num:1},function (data) {
Do something
}, ' get ');



Way like jquery oh ... Still think this kind of call convenient ...
Copy Code code as follows:

var ajax = {
XMLHttpRequest class
Xmlhttprequest:function () {
THIS.XHR =false;
},
External calling interface
Request:function (Url,data,callback,type) {
Each time a XMLHttpRequest object is created so that the AJAX call does not affect
var xhr = new this. XMLHttpRequest ();
Xhr.request (Url,data,callback,type);
}
}
convert {num:1,t: ' A '} This JSON data format to num=1&t=a this string form
var json2str = function (data) {
var _data = [];

for (var name in data) {
_data.push (name+ "=" +data[name]);
}
Return _data.join (' & ');
}
Ways to extend 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);
}
}


This class, there must be insufficient, welcome to the Brick Bar! Everyone has their own habits, the most important is the right to use it!
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.