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