This example describes the JavaScript Cross-domain request wrapper function and usage. Share to everyone for your reference, specific as follows:
First, the source code
Defines the JSON (function () {if (typeof window. $JSON = = ' undefined ') {window. $JSON = {} for Ajax Cross-domain requests;
}; $JSON. _ajax = function (config) {config = config[0] | |
{}; This.url = Config.url | |
''; This.type = Config.type | |
' XHR '; This.method = (This.type = = ' json ')? ' Get ': Config.method.toUpperCase () | |
' Get '; This.param = Config.param | |
Null This.callback = Config.callback | |
{}; This.
XHR = null;
if (typeof window._$json_callback = = ' undefined ') {window._$json_callback = {};
} this._createrequest ();
}; $JSON. _ajax.prototype = {//cache XHR request, no longer be judged when called again _createxhr:function () {var methods = [Function ( {return new XMLHttpRequest ();}, function () {return new ActiveXObject (' Msxml2.xmlhttp ');}, function () { Return to new ActiveXObject (' microsoft.xmlhttp ');
}
];
for (var i = 0, L = methods.length i < l i++) {try{methods[i] ();
}catch (e) {continue; }
THIS._CREATEXHR = Methods[i];
return methods[i] (); ()},//Establish XHR request _createrequest:function () {return (This.type = = ' json ')? This._setjsonrequest (): Thi
S._setxhrrequest ();
}, _setxhrrequest:function () {var _this = this;
var param = ';
for (var i in This.param) {if (param = = ") {param = i+ ' = ' +this.param[i];
}else{param+= ' & ' +i+ ' = ' +this.param[i]; }} this.
XHR = THIS._CREATEXHR (); This. Xhr.onreadystatechange = function () {if (_this). Xhr.readystate = = 4 && _this. Xhr.status = = {_this.callback.success (_this).
Xhr.responsetext);
}else{_this.callback.failure (' re-operation ');
}
}; This.
Xhr.open (This.method, This.url, true); This.
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8"); This.
Xhr.send (param); },//create JSON request _setjsonrequest:functioN () {var head = document.getElementsByTagName (' head ') [0];
var script = document.createelement (' script ');
var fun = This._setrandomfun ();
var _this = this;
var param = ';
for (var i in This.param) {if (param = = ") {param = i+ ' = ' +this.param[i];
}else{param+= ' & ' +i+ ' = ' +this.param[i];
} script.type = ' text/javascript ';
Script.charset = ' utf-8 ';
if (head) {head.appendchild (script);
}else{document.body.appendChild (script); }//Data: A callback function in the page that needs to be passed in for the callback function, as in the example "Jsonpcallback ()" method window._$json_callback[fun.id] = function (data)
{_this.callback.success (data);
settimeout (function () {delete window._$json_callback[fun.id];
Script.parentNode.removeChild (script);
}, 100);
};
SCRIPT.SRC = this.url+ '? callback= ' +fun.name+ ' & ' +param; },//Generate random JSON callback function _setrandomfun:functIon () {var id = ';
do{id = ' $JSON ' +math.floor (Math.random () *10000); }while (Window._$json_callback[id]) return{id:id, Name: ' Window._$json_callback. '
+id}};
window. $JSON. Ajax = function () {return new $JSON. _ajax (arguments);
}
})();
Second, the way to call
Invocation mode
var ajax = new $JSON. Ajax ({
URL: ' Http://www.sina.com/api ',
type: ' JSON ', method
:
' get ', Param: {
bar:true
},
callback: {
success:function (data) {
console.log (' 55668 ', data);
} ,
failure:function (Error) {
alert (errow);
}
}
);
More readers interested in JavaScript-related content can view this site: "JavaScript Ajax Operating Skills Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"
I hope this article will help you with JavaScript programming.