jquery ajax忽略500状态错误
我正在向 App Engine 应用程序发出一些 GET 请求,并在 Chrome 中进行测试。虽然我可以在 javascript 控制台中看到某些调用导致 500 服务器错误,但我似乎无法在 jQuery 代码中找到捕获此错误的方法,尽管阅读了许多类似的 SO 线程。我知道它表明服务器端错误,但我仍然希望能够从我的 JavaScript 捕获这样的错误。
我需要捕获错误,以便可以计算响应的数量(成功或失败),并在收到所有调用响应时触发另一个函数。
Chrome 控制台输出:
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);
}
},
});
}
}
我已尝试以下一些操作:
添加:
statusCode: {500: function() {alert('err');}}
到通话中。
使用:
$().ready(function(){
$.ajaxSetup({
error:function(x,e) {
if(x.status==500) {
alert('Internel Server Error.');
}
}
});
});
有人对我如何捕获 500 响应有建议吗?
谢谢 Oli
更新:
根据响应,我的 jquery 代码似乎是正确的,但由于某种原因,它只能捕获从我的应用程序收到的某些 500 个响应。这可能是 App Engine 如何返回错误的问题(我对此了解不多),或者 jquery 如何使用 jsonp 处理错误 - 这一点在 这篇文章。
我通过使用 jquery-isonp 来实现此功能,它捕获了应用程序抛出的所有 500 状态。
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¶ms=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<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');}}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您没有正确使用 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:or the shorthand:
通过将 jQuery 从 1.9.1 升级到 2.1.1 解决了这个问题 - 现在它开始在服务器响应上调用
.error()
(之前它会忽略 500 响应并等待超时结束) 。限制:jQuery 2.1.1 不支持 IE8 及更低版本(IMO 不应该支持)
Solved the problem by upgrading jQuery from 1.9.1 to 2.1.1 — now it started calling
.error()
right on the server response (before it would ignore the 500 response and wait until timeout is over).Limitation: jQuery 2.1.1 does not support IE8 and below (as IMO shouldn't you)
我有一个类似的问题,我正在使用 jquery 的 Promise 函数 .done、.fail、.always,如果我遇到 500 内部服务器错误,那么它不会触发任何函数(done、fail、always、error)。很奇怪。
最后,我在 .ajax 选项中添加了一个超时,当它达到超时时,它会抛出一个错误并运行 .fail 方法。
I had a similar problem, I was using jquery's promise functions of .done, .fail, .always, and if I encountered a 500 internal server error then it would not fire any of the functions (done, fail, always, error). very weird.
in the end I added a timeout into the .ajax options, when it hits the timeout it throws an error and also runs the .fail method.