jQuery ajax忽略500状态错误 [英] jquery ajax ignores 500 status error
问题描述
我正在向App Engine应用程序发出一些GET请求,并在Chrome中进行了测试.虽然我可以在javascript控制台中看到某些调用导致500服务器错误,但是尽管阅读了许多类似的SO线程,但我似乎还是找不到在jQuery代码中捕获此错误的方法.我了解它表示服务器端错误,但我仍然希望能够从我的JavaScript中捕获此类错误.
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控制台输出:
Chrome console output:
GET http://myapp.com/api?callback=jQuery12345¶ms=restOfParams 500 (Internal Server Error)
我的电话:
function makeCall() {
var count = 0;
var alldata = $('#inputset').val();
var rows = alldata.split('\n');
var countmatch = rows.length;
for (i=0;i<rows.length;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');}}
通话.
使用:
$().ready(function(){
$.ajaxSetup({
error:function(x,e) {
if(x.status==500) {
alert('Internel Server Error.');
}
}
});
});
有人会对我如何获得500条回应提出建议吗?
Would anyone have a suggestion regarding how I could catch the 500 response?
谢谢 奥利(Oli)
更新:
基于响应,我的jquery代码似乎是正确的,但是由于某种原因,它只能捕获从我的应用收到的某些500个响应.这可能是App Engine如何返回错误(我对此不太了解)或jquery如何处理jsonp错误的问题-这一点在
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.
我通过使用 jquery-isonp 使它起作用,该捕获器捕获了应用程序.
I got this to work by using jquery-isonp which caught all of the 500 status's thrown by the app.
推荐答案
您似乎没有正确使用jQuery的document.ready
绑定. $().ready(...)
版本或多或少不推荐使用.尝试以下方法之一:
It doesn't look like you're using jQuery's document.ready
binding correctly. The $().ready(...)
version is more-or-less deprecated. Try one of these instead:
$(document).ready(function() {
$.ajaxSetup({
error: function(x, e) {
if (x.status == 500) {
alert('Internel Server Error.');
}
}
});
});
或速记:
$(function() {
$.ajaxSetup({
error: function(x, e) {
if (x.status == 500) {
alert('Internel Server Error.');
}
}
});
});
这篇关于jQuery ajax忽略500状态错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!