This article mainly introduces information about the global configuration of Ajax requests encapsulated by jQuery. For more information, see
Abstract:
JQuery has become the most common js library in the project and is also the favorite library for front-end development. The following describes how to encapsulate jQuery Ajax in the project and share it with you.
Code:
The Code is as follows:
// Ajax Request Parameters
Var ajaxSettings = function (opt ){
Var url = opt. url;
Var href = location. href;
// Determine whether a cross-origin request is required
Var requestType = 'jsonp ';
If (url. indexOf (location. host)>-1)
RequestType = 'json ';
RequestType = opt. dataType | requestType;
// Asynchronous request
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 determine whether the returned data is in json format? If not
* General Format of successful data
*{
* "Code": 200,
* "Data": [],
* "Success": true // success
*}
* Data returned from failure
*{
* "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 | pide | #247 ); /g,
Entity = {
'':'',
'':'',
'<': '<',
'<': '<',
'>': '> ',
'& 62;': '> ',
'&':'&',
'&':'&',
'"':'"',
'"':'"',
'Handler': 'handler ',
'Handler': 'handler ',
'£': '£ ',
'£': '£ ',
'000000': '000000 ',
'000000': '000000 ',
'€ ':'? ',
'€ ':'? ',
'§':'§',
'§':'§',
'©':'©',
'©':'©',
'®':'®',
'®':'®',
'™':'™',
'™':'™',
'×':' × ',
'×':' × ',
'& Pide;': 'region ',
'Handler': 'handler'
};
If (str = null ){
Return '';
}
Str = str. toString ();
Return str. indexOf (';') <0? Str: str. replace (reg, function (chars ){
Return entity [chars];
});
}
// Convert html Objects
$. AjaxSetup ({
Global: true,
Cache: false,
Converters :{
'Text json': function (response ){
Return jQuery. parseJSON (unescapeEntity (response ));
}
}
});
/*
* The Ajax request permission is incorrect.
* User permission error jump to the login page
* 404 error redirected to the 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
* Example: Error 500
* Returned error message format
*{
* Code: 500,
* Info: System 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 );
});
Summary:
To execute an Ajax request, you only need to call the ajaxSettings function, as shown below:
The Code is as follows:
$. Ajax (ajaxSettings ({
Url :'',
Data :''
}))
The above is all the content of this article. I hope you will like it.