如何处理getJSON调用中的错误?我试图引用一个跨域脚本服务使用jsonp,你如何注册一个错误方法?
当前回答
这是我的加法。
来自http://www.learnjavascript.co.uk/jq/reference/ajax/getjson.html和官方来源
jqXHR.success()、jqXHR.error()和jqXHR.complete()回调 jQuery 1.5中引入的方法在jQuery 1.8中已弃用。来 为最终删除它们准备代码,使用jqXHR.done(), jqXHR.fail()和jqXHR.always()代替。”
我这样做了,这里是Luciano的更新代码片段:
$.getJSON("example.json", function() {
alert("success");
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function() { alert('getJSON request failed! '); })
.always(function() { alert('getJSON request ended!'); });
错误描述加上显示所有json数据为字符串:
$.getJSON("example.json", function(data) {
alert(JSON.stringify(data));
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { alert('getJSON request failed! ' + textStatus); })
.always(function() { alert('getJSON request ended!'); });
如果您不喜欢警告,可以用console.log代替它们
$.getJSON("example.json", function(data) {
console.log(JSON.stringify(data));
})
.done(function() { console.log('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { console.log('getJSON request failed! ' + textStatus); })
.always(function() { console.log('getJSON request ended!'); });
其他回答
在某些情况下,使用这种方法可能会遇到同步问题。 我在setTimeout函数中编写了回调调用,它可以同步工作=)
E.G:
function obterJson(callback) {
jqxhr = $.getJSON(window.location.href + "js/data.json", function(data) {
setTimeout(function(){
callback(data);
},0);
}
$. getjson()是一种常规AJAX调用的抽象,在这种调用中,您必须告诉您需要一个JSON编码的响应。
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
可以用两种方式处理错误:一般地(在实际调用AJAX调用之前配置它们)或特定地(使用方法链)。
“generic”是这样的:
$.ajaxSetup({
"error":function() { alert("error"); }
});
以及“具体”的方式:
$.getJSON("example.json", function() {
alert("success");
})
.done(function() { alert("second success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
有人能告诉卢西亚诺以下几点吗? 我刚刚测试了他的答案——有一个类似的问题——结果非常完美……
我甚至还加上了我的50美分:
.error(function(jqXHR, textStatus, errorThrown) {
console.log("error " + textStatus);
console.log("incoming Text " + jqXHR.responseText);
})
为什么不
getJSON('get.php',{cmd:"1", typeID:$('#typesSelect')},function(data) {
// ...
});
function getJSON(url,params,callback) {
return $.getJSON(url,params,callback)
.fail(function(jqXMLHttpRequest,textStatus,errorThrown) {
console.dir(jqXMLHttpRequest);
alert('Ajax data request failed: "'+textStatus+':'+errorThrown+'" - see javascript console for details.');
})
}
??
有关使用的.fail()方法(jQuery 1.5+)的详细信息,请参见http://api.jquery.com/jQuery.ajax/#jqXHR
由于jqXHR是由函数返回的,因此类似于
$.when(getJSON(...)).then(function() { ... });
是可能的。
这是我的加法。
来自http://www.learnjavascript.co.uk/jq/reference/ajax/getjson.html和官方来源
jqXHR.success()、jqXHR.error()和jqXHR.complete()回调 jQuery 1.5中引入的方法在jQuery 1.8中已弃用。来 为最终删除它们准备代码,使用jqXHR.done(), jqXHR.fail()和jqXHR.always()代替。”
我这样做了,这里是Luciano的更新代码片段:
$.getJSON("example.json", function() {
alert("success");
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function() { alert('getJSON request failed! '); })
.always(function() { alert('getJSON request ended!'); });
错误描述加上显示所有json数据为字符串:
$.getJSON("example.json", function(data) {
alert(JSON.stringify(data));
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { alert('getJSON request failed! ' + textStatus); })
.always(function() { alert('getJSON request ended!'); });
如果您不喜欢警告,可以用console.log代替它们
$.getJSON("example.json", function(data) {
console.log(JSON.stringify(data));
})
.done(function() { console.log('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { console.log('getJSON request failed! ' + textStatus); })
.always(function() { console.log('getJSON request ended!'); });