ajax status 500,jquery ajax ignores 500 status error

在使用jQuery进行GET请求时,遇到500内部服务器错误无法被捕获的问题。尝试了在AJAX设置中添加statusCode和使用全局ajaxError处理函数,但只对部分500响应起作用。问题可能与AppEngine返回错误的方式或jQuery处理JSONP错误有关。最终通过使用jquery-isonp库成功捕获所有500状态响应。
摘要由CSDN通过智能技术生成

I'm making some GET requests to an App Engine app, testing in Chrome. Whilst I can see in javascript console that some calls result in a 500 server error, I can't seem to find anyway of capturing this error in my jQuery code despite reading a number of similar SO threads. I understand that it indicates a server side error, but I'd still like to be able to capture such an error from my javascript.

I need to capture the error so that I can count the number of responses (successful or otherwise) and trigger another function when all call responses have been received.

Chrome console output:

GET http://myapp.com/api?callback=jQuery12345&params=restOfParams 500 (Internal Server Error)

My call:

function makeCall() {

var count = 0;

var alldata = $('#inputset').val();

var rows = alldata.split('\n');

var countmatch = rows.length;

for (i=0;i

data["param"] = rows[i]["val"];

$.ajax({

url: apiUrl,

type: 'GET',

data: data,

dataType: 'jsonp',

error: function(){

alert('Error loading document');

count +=1;

},

success: function(responseJson) {

count +=1;

var res = responseJson.results;

if (count == countmatch) {

allDoneCallback(res);

}

},

});

}

}

I've tried some of the following:

Adding:

statusCode: {500: function() {alert('err');}}

to the call.

Using:

$().ready(function(){

$.ajaxSetup({

error:function(x,e) {

if(x.status==500) {

alert('Internel Server Error.');

}

}

});

});

Would anyone have a suggestion regarding how I could catch the 500 response?

Thanks

Oli

UPDATE:

Based on responses, my jquery code appears to be correct, but for some reason it would only catch certain 500 responses received from my app. This is possibly a problem with how App Engine returns the error(I don't know a lot about this), or how jquery handles errors with jsonp - this point is briefly discussed in the last paragraph of this article.

I got this to work by using jquery-isonp which caught all of the 500 status's thrown by the app.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值