·â×°ÁËjQueryµÄAjaxÇëÇóÈ«¾ÖÅäÖÃ_Javascript教程-查字典教程网
·â×°ÁËjQueryµÄAjaxÇëÇóÈ«¾ÖÅäÖÃ
·â×°ÁËjQueryµÄAjaxÇëÇóÈ«¾ÖÅäÖÃ
发布时间:2016-12-30 来源:查字典编辑
摘要:ÕªÒª£º¡¡¡¡jQueryÒѾ­³ÉΪÏîÄ¿ÖÐ×î³£¼ûµÄjs¿â£¬Ò²ÊÇÇ°¶Ë¿ª·¢×îϲ»¶Ê¹ÓõĿâ...

ͻ񻣼

¡¡¡¡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¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íû´ó¼ÒÄܹ»Ï²»¶¡£

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类