简体   繁体   English

在跨域请求上捕获JSONP错误

[英]Catching a JSONP error on a cross-domain request

I'm using jQuery.getJSON() on a URL (different domain) which may not exist. 我在URL(不同的域)上使用jQuery.getJSON()可能不存在。 Is there a way for me to catch the error "Failed to load resource"? 有没有办法让我捕获错误“无法加载资源”? It seems that try/catch doesn't work because of the asynchronous nature of this call. 由于此调用的异步性质,似乎try / catch不起作用。

I can't use jQuery.ajax() 's " error: " either. 我也不能使用jQuery.ajax()的“ error: ”。 From the documetation: 从文献:

Note: This handler is not called for cross-domain script and JSONP requests. 注意:不会为跨域脚本和JSONP请求调用此处理程序。

If you have an idea of the worst case delay of a successful result returning from the remote service, you can use a timeout mechanism to determine if there was an error or not. 如果您知道从远程服务返回的成功结果的最坏情况延迟,则可以使用超时机制来确定是否存在错误。

var cbSuccess = false;
$.ajax({
   url: 'http://example.com/.../service.php?callback=?',
   type: 'get',
   dataType: 'json',
   success: function(data) {
              cbSuccess = true;
            }
});
setTimeout(function(){ 
        if(!cbSuccess) { alert("failed"); } 
    }, 2000); // assuming 2sec is the max wait time for results

This works: 这有效:

j.ajaxSetup({
    "error":function(xhr, ajaxOptions, thrownError) {   

    console.log(thrownError);                           
}});

Deferred objects (new in jQuery 1.5) sound like exactly what you're looking for: 延迟对象(jQuery 1.5中的新对象)听起来就像你正在寻找的那样:

jQuery.Deferred, introduced in version 1.5, is a chainable utility object that can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function. 在1.5版本中引入的jQuery.Deferred是一个可链接的实用程序对象,它可以将多个回调注册到回调队列,调用回调队列,并中继任何同步或异步函数的成功或失败状态。

http://api.jquery.com/category/deferred-object/ http://api.jquery.com/category/deferred-object/

EDIT: 编辑:

The following code works fine for me: 以下代码对我来说很好:

function jsonError(){
    $("#test").text("error");
}

$.getJSON("json.php",function(data){
    $("#test").text(data.a);
}).fail(jsonError);

json.php looks like this: json.php看起来像这样:

print '{"a":"1"}';

The error function fires for me if the path to json.php is incorrect or if the JSON is malformed. 如果json.php的路径不正确或者JSON格式错误,则会触发error函数。 For example: 例如:

print '{xxx"a":"1"}';

What your complaining about is a client-side error saying that you tried to download a resource from the server. 你抱怨的是客户端错误,说你试图从服务器下载资源。

This is build into the browser and it allows your browser to tell the client or the developers that the downloading of a resource from the internet failed. 这是构建到浏览器中,它允许您的浏览器告诉客户端或开发人员从Internet下载资源失败。 This has nothing to do with JavaScript and is a more low level error thrown on the HTTP that is caught by the browser. 这与JavaScript无关,并且是由浏览器捕获的HTTP引发的更低级错误。

If you want any hope of catching it your going to need to drop 3rd party ajax libraries and deal with the XMLHTTPRequest object on a much lower level, even then I doubt you can do anything about it. 如果你想要捕获它,你需要放弃第三方ajax库并在更低的层次上处理XMLHTTPRequest对象,即使那时我怀疑你可以做任何事情。

Basically when you see this error then find out what object your trying to get that doesn't exist or couldn't be accessed. 基本上,当您看到此错误时,请找出您尝试获取的不存在或无法访问的对象。 Then either stop accessing it or make it accessible. 然后停止访问它或使其可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM