jquery has become the most common JS library in the project, and it is also the most favorite library in the front-end development. Here's the Ajax that encapsulates the jquery in the project and share it with everyone.
Ajax Request Parameters
var ajaxsettings = function (opt) {
var url = opt.url;
var href = location.href;
To determine whether Cross-domain requests
var RequestType = ' Jsonp ';
if (Url.indexof (Location.host) >-1)
RequestType = ' json ';
RequestType = Opt.datatype | | RequestType;
Whether to request asynchronously
var async = (Opt.async = = = undefined? true:opt.async);
return {
Url:url,
Async:async,
Type:opt.type | | ' Get ',
Datatype:requesttype,
Cache:false,
Data:opt.data,
Success:function (data, Textstatus, XHR) {
/*
* If datatype is JSON, how can I tell if the returned data is in JSON format, if not converted
* Successful Data General format
* {
* "Code": 200,
* "Data": [],
* "Success": TRUE//Success
* }
* Failed to return data
* {
* "Code": 200,
* "Info": ' Error ',
* "Success": false//Failed
* }
*/
if ((RequestType = = ' json ' | | requesttype = = "Jsonp") && typeof (data) = = "string") {
data = json.parse (data);
}
if (data.success) {
Opt.success (data);
}
if (opt.error) {
Opt.error (data);
}
},
Error:function (XHR, status, Handler) {
if (opt.error)
Opt.error ();
}
};
};
function Unescapeentity (str) {
var reg =/& (?: nbsp| #160 |lt| #60 |gt|62|amp| #38 |quot| #34 |cent| #162 |pound| #163 |yen| #165 |euro| #8364 |sect| #167 | copy| #169 |reg| #174 |trade| #8482 |times| #215 |divide| #247)/g,
entity = {
' ' : ' ',
' ' : ' ',
' < ': ' < ',
' < ': ' < ',
' > ': ' > ',
' &62; ' : ' > ',
' & ': ' & ',
' & ': ' & ',
'"' : '"',
'"' : '"',
' ¢ ': ' ¢ ',
' ¢ ': ' ¢ ',
'£' : '£',
'£' : '£',
'¥' : '¥',
'¥' : '¥',
' € ': '? ',
' € ': '? ',
'§' : '§',
'§' : '§',
'©' : '©',
'©' : '©',
'®' : '®',
'®' : '®',
' TM ': ' ™ ',
' TM ': ' ™ ',
' x ': ' X ',
' x ': ' X ',
' A ': '
' A ': '
};
if (str = = null) {
Return ";
}
str = str.tostring ();
return Str.indexof (';') < 0? Str:str.replace (Reg, function (chars) {
return Entity[chars];
});
}
Entities that convert HTML
$.ajaxsetup ({
Global:true,
Cache:false,
Converters: {
' Text JSON ': function (response) {
Return Jquery.parsejson (unescapeentity (response));
}
}
});
/*
*ajax Request Permission exception
* User Rights error Jump landing page
* 404 Error jump 404 page
*/
$ (document). Ajaxcomplete (function (evt, req, settings) {
if (req && Req.responsejson) {
var json = Req.responsejson;
if (Json.code = = 403 && Json.info = = ' perm error ' &&!json.success) {
Window.location.href = Location.protocol + '//' + location.hostname;
Return
}
if (Json.code = = 404 &&!json.success) {
Window.location.href = Location.protocol + '//' + location.hostname + '/404.html ';
}
}
});
/*
*ajax Request Error Prompt
* For example: 500 error
* Return error message Format
*{
* code:500,
* Info: The system has an exception
*}
*/
$ (document). Ajaxerror (function (evt, req, settings) {
if (req && (req.status = = 200| | Req.status = = 0)) {return false;}
var msg = ' ERROR: ';
if (req && Req.responsejson) {
var json = Req.responsejson;
msg + + json.code| | ';
msg + json.info| | ' System exception, please retry ';
}else{
msg = ' System exception, please try again ';
}
Alert (msg);
});
You only need to invoke the Ajaxsettings function when executing an AJAX request, as follows:
The above mentioned is the entire content of this article, I hope you can enjoy.