Global configuration of Ajax requests and global ajax requests

Source: Internet
Author: User

Global configuration of Ajax requests and global ajax requests
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:
// Ajax request parameter var ajaxSettings = function (opt) {var url = opt. url; var href = location. href; // determine whether cross-origin request var requestType = 'jsonp'; if (url. indexOf (location. host)>-1) requestType = 'json'; requestType = opt. dataType | requestType; // whether to asynchronously request var async = (opt. async === undefined? True: opt. async); return {url: url, async: async, type: opt. type | 'get', ype: 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 conversion is not performed * General data Format * {* "code": 200, * "data": [], * "success ": true // success *} * returned data upon 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 | divide | #247 ); /g, entity = {'& nbsp;': '', '& #160;':'', '& lt;': '<', '& #60; ':' <',' & gt; ':'> ',' & 62; ':'> ',' & amp ;':'&', '& #38;': '&', '& quot;': '"', '& #34;': '"', '& cent ;': 'hangzhou', '& #162;': 'hangzhou', '& pound;': '£', '& #163;': '£', '& yen; ': '000000',' & #165; ': '000000',' & euro; ':' €', '& #8364;': '€ ', '& sect;': '§', '& #167;': '§', '& copy ;':'©',' & #169 ;':'©',' & Reg ;':'®',' & #174 ;':'®',' & Trade ;':'™',' & #8482 ;':'™',' & Times; ':' × ',' & #215; ':' × ',' & divide; ': 'region',' & #247 ;': 'condition'}; if (str = null) {return '';} str = str. toString (); return str. indexOf (';') <0? Str: str. replace (reg, function (chars) {return entity [chars] ;}) ;}// convert the html entity $. ajaxSetup ({global: true, cache: false, converters: {'text json': function (response) {return jQuery. parseJSON (unescapeEntity (response) ;}});/** Ajax request permission exception * user permission error jump to login page * 404 error jump to 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 * 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 try again ';} 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:

$.ajax(ajaxSettings({    url: '',    data: ''}))

 

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.