¡¡¡¡ÕªÒª£º
¡¡¡¡
¡¡¡¡jQueryÒѾ³ÉΪÏîÄ¿ÖÐ×î³£¼ûµÄjs¿â£¬Ò²ÊÇǰ¶Ë¿ª·¢×îϲ»¶Ê¹ÓõĿ⡣ÏÂÃæÊÇÔÚÏîÄ¿Öзâ×°ÁËjQueryµÄAjax£¬·ÖÏí¸ø´ó¼Ò¡£
¡¡¡¡´úÂ룺
¡¡¡¡
¡¡¡¡// ajax ÇëÇó²ÎÊý
¡¡¡¡var ajaxSettings = function(opt) {
¡¡¡¡var url = opt.url;
¡¡¡¡var href = location.href;
¡¡¡¡// ÅжÏÊÇ·ñ¿çÓòÇëÇó
¡¡¡¡var requestType = 'jsonp';
¡¡¡¡if (url.indexOf(location.host) > -1)
¡¡¡¡requestType = 'json';
¡¡¡¡requestType = opt.dataType || requestType;
¡¡¡¡// ÊÇ·ñÒì²½ÇëÇó
¡¡¡¡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) {
¡¡¡¡/*
¡¡¡¡*Èç¹ûdataTypeÊÇjson£¬ÔõÅжϷµ»ØÊý¾ÝÊÇ·ñΪjson¸ñʽ£¬Èç¹û²»ÊǽøÐÐת»»
¡¡¡¡* ³É¹¦Êý¾ÝͨÓøñʽ
¡¡¡¡* {
¡¡¡¡* "code": 200,
¡¡¡¡* "data": [],
¡¡¡¡* "success": true // ³É¹¦
¡¡¡¡* }
¡¡¡¡* ʧ°Ü·µ»ØµÄÊý¾Ý
¡¡¡¡* {
¡¡¡¡* "code": 200,
¡¡¡¡* "info": 'error',
¡¡¡¡* "success": false // ʧ°Ü
¡¡¡¡* }
¡¡¡¡*/
¡¡¡¡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;' : '>',
¡¡¡¡'&' : '&',
¡¡¡¡'&' : '&',
¡¡¡¡'"' : '"',
¡¡¡¡'"' : '"',
¡¡¡¡'¢' : '¡é',
¡¡¡¡'¢' : '¡é',
¡¡¡¡'£' : '£',
¡¡¡¡'£' : '£',
¡¡¡¡'¥' : '¥',
¡¡¡¡'¥' : '¥',
¡¡¡¡'€' : '€',
¡¡¡¡'€' : '€',
¡¡¡¡'¡ì' : '¡ì',
¡¡¡¡'¡ì' : '¡ì',
¡¡¡¡'©' : '©',
¡¡¡¡'©' : '©',
¡¡¡¡'®' : '®',
¡¡¡¡'®' : '®',
¡¡¡¡'™' : '™',
¡¡¡¡'™' : '™',
¡¡¡¡'¡Á' : '¡Á',
¡¡¡¡'¡Á' : '¡Á',
¡¡¡¡'¡Â' : '¡Â',
¡¡¡¡'¡Â' : '¡Â'
¡¡¡¡};
¡¡¡¡if (str === null) {
¡¡¡¡return '';
¡¡¡¡}
¡¡¡¡str = str.toString();
¡¡¡¡return str.indexOf(';') < 0 ? str : str.replace(reg, function(chars) {
¡¡¡¡return entity[chars];
¡¡¡¡});
¡¡¡¡}
¡¡¡¡// ת»»htmlµÄʵÌå
¡¡¡¡$.ajaxSetup({
¡¡¡¡global : true,
¡¡¡¡cache : false,
¡¡¡¡converters : {
¡¡¡¡'text json' : function(response){
¡¡¡¡return jQuery.parseJSON(unescapeEntity(response));
¡¡¡¡}
¡¡¡¡}
¡¡¡¡});
¡¡¡¡/*
¡¡¡¡*Ajax ÇëÇóȨÏÞÒì³£
¡¡¡¡* Óû§È¨ÏÞ´íÎóÌø×ªµÇ½ҳ
¡¡¡¡* 404´íÎóÌø×ª404Ò³Ãæ
¡¡¡¡*/
¡¡¡¡$(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 ÇëÇó´íÎóÌáʾ
¡¡¡¡*ÀýÈ磺500´íÎó
¡¡¡¡*·µ»Ø´íÎóÐÅÏ¢¸ñʽ
¡¡¡¡*{
¡¡¡¡* code: 500,
¡¡¡¡* info: ϵͳ·¢ÉúÒì³£
¡¡¡¡*}
¡¡¡¡*/
¡¡¡¡$(document).ajaxError(function(evt, req, settings){
¡¡¡¡if(req && (req.status === 200||req.status === 0)){ return false; }
¡¡¡¡var msg = '´íÎó£º';
¡¡¡¡if(req && req.responseJSON){
¡¡¡¡var json = req.responseJSON;
¡¡¡¡msg += json.code||'';
¡¡¡¡msg += json.info||'ϵͳÒì³££¬ÇëÖØÊÔ';
¡¡¡¡}else{
¡¡¡¡msg = 'ϵͳÒì³££¬ÇëÖØÊÔ';
¡¡¡¡}
¡¡¡¡alert(msg);
¡¡¡¡});
¡¡¡¡Ð¡½á£º
¡¡¡¡ÔÚÖ´ÐÐAjaxÇëÇóʱֻÐèÒªµ÷ÓÃajaxSettingsº¯Êý¼´¿É£¬ÈçÏ£º
¡¡¡¡
¡¡¡¡$.ajax(ajaxSettings({
¡¡¡¡url: '',
¡¡¡¡data: ''
¡¡¡¡}))
¡¡¡¡ÒÔÉÏËùÊö¾ÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íû´ó¼ÒÄܹ»Ï²»¶¡£