如何在jQuery getJSON调用中捕获400响应

在jQuery中,我想使用$.getJSON()方法从Facebook获取一些数据,但如果令牌无效,Facebook将返回400状态。 我如何捕获$.getJSON()而不是$.ajax()

我认为这对你有用

 $.getJSON("example.json", function() { alert("success"); }) .success(function() { alert("success 2"); }) .error(function() { alert("error occurred "); }) .complete(function() { alert("Done"); }); 

jquery Ajax文档提供了两种解决方案,第一种是错误函数:

  error(jqXHR, textStatus, errorThrown) 

它为您检测并报告错误消息的文本部分,另一个是状态代码function(在同一页面上)。 以下是该页面的示例用法:

  $.ajax({ statusCode: { 404: function() { alert("page not found"); } } }); 

使用complete(jqXHR, textStatus)函数回调并调查响应并向用户显示approprite消息。