jQuery:处理 getJSON() 中的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5388934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: handle errors in getJSON()?
提问by AP257
How do I handle 500 errors when using jQuery's getJSON?
使用 jQuery 的getJSON时如何处理 500 错误?
There have been a couple of questions about error handling with getJSON()
andJSONP, but I'm not working with JSONP, just ordinary JSON.
有几个关于使用getJSON()
和JSONP 进行错误处理的问题,但我没有使用 JSONP,只是使用普通的 JSON。
Another answersuggests using .ajaxSetup()
before calling getJSON()
, so I tried this:
另一个答案建议.ajaxSetup()
在调用之前使用getJSON()
,所以我尝试了这个:
$.ajaxSetup({
"error":function() {
alert('Error!');
}});
$.getJSON('/book_results/', function(data) { # etc
But I find that the alert always triggers, even when the result is well-formed.
但我发现警报总是会触发,即使结果格式正确。
Any ideas?
有任何想法吗?
回答by halfpastfour.am
The getJSON
method does not natively return errors but you could dive into the xhr object that is returned as a parameter in the callback.
该getJSON
方法本身不会返回错误,但您可以深入研究在回调中作为参数返回的 xhr 对象。
The getJSON
method is a shorthand function for jQuery.ajax
. Using jQuery.ajax
you can easily achieve error handling:
该getJSON
方法是 的简写函数jQuery.ajax
。使用jQuery.ajax
你可以轻松实现错误处理:
$.ajax({
url: 'http://127.0.0.1/path/application.json',
dataType: 'json',
success: function( data ) {
alert( "SUCCESS: " + data );
},
error: function( data ) {
alert( "ERROR: " + data );
}
});
回答by bibstha
If you are using the jquery version 1.5 or higher you can use the new methods .success(function), .error(function) and .complete(function)
如果您使用的是 jquery 1.5 或更高版本,则可以使用新方法 .success(function)、.error(function) 和 .complete(function)
Example from http://api.jquery.com/jQuery.get/
来自http://api.jquery.com/jQuery.get/ 的示例
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.get("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
Works perfect for me. I hope this helps
非常适合我。我希望这有帮助
回答by lee
you can see it on jquery api getJSON: http://api.jquery.com/jQuery.getJSON/
你可以在 jquery api getJSON 上看到它:http: //api.jquery.com/jQuery.getJSON/
$.getJSON(url).done(function(data){
$("#content").append(data.info);
})
.fail(function(jqxhr){
alert(jqxhr.responseText);
});
//jquery1.5+the fail callback will trigger when text is not the correct json string or any other fail solutions
// jquery1.5+当文本不是正确的 json 字符串或任何其他失败解决方案时,将触发失败回调
回答by Kevin Struillou
Using jQuery 3.2.1:
使用 jQuery 3.2.1:
$.getJSON('/api/feed/update', function (data) {
console.log(data);
}).catch(function (jqXHR, textStatus, errorThrown) {
console.error(jqXHR);
console.error(textStatus);
console.error(errorThrown);
});
回答by user1258024
Please do the following. Pseduo code:
请执行以下操作。伪代码:
$.getJSON('/path/to_your_url.do?dispatch=toggle&' + new Date().getTime(), function(data, status, xhr){
if( xhr.status == 200 )
// all good and do your processing with 'data' parameter( your response)
else {
//error - check out the values for only in chrome for 'console'
console.log(xhr.status);
console.log(xhr.response);
console.log(xhr.responseText)
console.log(xhr.statusText);
}
}