ajax json 404,如何用jQuery回调JSON ajax请求中的404函数?

这篇博客探讨了如何利用jQuery的Ajax方法处理错误,包括检查错误响应中特定字符串来判断错误类型,并进行相应操作,如重定向到登录页面或显示错误信息。同时,还介绍了两个辅助函数,一个是用于检测错误数据,另一个用于报告失败并展示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的第一个电话:

$.ajax({ type: 'POST',

url: '../doSomething',

data: 'my data',

success: function(data) {

if (HasErrors(data)) return;

var info = eval('(' + data + ')');

// do what you want with the info object

},

error: function(xmlHttpRequest) {

ReportFailure(xmlHttpRequest);

}

});

以及两个helper函数:

function HasErrors(data) {

if (data.search(/login\.aspx/i) != -1) {

// timed out and being redirected to login page!

top.location.href = '../login.aspx';

return true;

}

if (data.search(/Internal Server Error/) != -1) {

ShowStatusFailed('Server Error.');

return true;

}

if (data.search(/Error.aspx/) != -1) {

// being redirected to site error reporting page...

ShowStatusFailed('Server Error. Please try again.');

return true;

}

return false;

}

function ReportFailure(msg) {

var text;

if (typeof msg == 'string') {

text = msg;

}

else if (typeof msg.statusText == 'string') {

if (msg.status == 200) {

text = msg.responseText;

}

else {

text = '(' + msg.status + ') ' + msg.statusText + ': ';

// use the Title from the error response if possible

var matches = msg.responseText.match(/\

(.*?)\/i);

if (matches != null)

{ text = text + matches[1]; }

else

{ text = text + msg.responseText; }

}

}

// do something in your page to show the "text" error message

$('#statusDisplay')

.html('' + text)

.addClass('StatusError');

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值