A simplified version of the JS Library analysis for Ajax

Source: Internet
Author: User
Tags jquery library

A simplified version of the Ajax jquery library

(function () {
The only variable interface for the outside world!
var Myajax = Window.myajax = {};
Author, version number, etc. information
Myajax.author = "Koala";
Myajax.version = "1.0.0";

This object has two properties, get, post properties, all functions
Myajax.get = function (url,queryjson,callback) {
Create XHR objects to resolve compatibility issues
if (window. XMLHttpRequest) {
var xhr = new XMLHttpRequest ();
}else{
var xhr = new ActiveXObject ("Microsoft.XMLHTTP");
}
Things to do after the results are returned
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
if (xhr.status >= && Xhr.status < | | xhr.status = 304) {
Callback (Null,xhr.responsetext);
}else{
Callback ("The requested file was not found") (new Error), undefined);
}
}
}
Stitching strings
var querystring = myajax._queryjson2querystring (Queryjson);
Configuration
Xhr.open ("Get", URL + "?" + querystring, True);
Send
Xhr.send (NULL);
};

This object has two properties, get, post properties, all functions
Myajax.post = function (url,queryjson,callback) {
Create XHR objects to resolve compatibility issues
if (window. XMLHttpRequest) {
var xhr = new window. XMLHttpRequest ();
}else{
var xhr = new ActiveXObject ("Microsoft.XMLHTTP");
}
Things to do after the results are returned
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
if (xhr.status >= && Xhr.status < | | xhr.status = 304) {
Callback (Null,xhr.responsetext);
}else{
Callback ("The requested file was not found") (new Error), undefined);
}
}
}
Stitching strings
var querystring = myajax._queryjson2querystring (Queryjson);
Configuration
Xhr.open ("Post", URL, True);
Send
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xhr.send (querystring);
};

intrinsic function, query JSON becomes query string
Enter a {"name": "Kaola", "age": +, "sex": "Male"}
Returns a Name=kaola&age=18&sex=%e8%c6%b6
myajax._queryjson2querystring = function (JSON) {
var arr = []; Result array
For (var k in JSON) {
Arr.push (k + "=" + encodeURIComponent (Json[k]));
}
Return Arr.join ("&");
};
})();

A simplified version of the JS Library analysis for Ajax

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.