Encapsulates jQuery's Ajax request global configuration _ jquery

Source: Internet
Author: User
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.

Related Article

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.