最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catching a JSONP error on a cross-domain request - Stack Overflow

programmeradmin1浏览0评论

I'm using jQuery.getJSON() on a URL (different domain) which may not exist. 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.

I can't use jQuery.ajax()'s "error:" either. From the documetation:

Note: This handler is not called for cross-domain script and JSONP requests.

I'm using jQuery.getJSON() on a URL (different domain) which may not exist. 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.

I can't use jQuery.ajax()'s "error:" either. From the documetation:

Note: This handler is not called for cross-domain script and JSONP requests.

Share Improve this question edited Nov 9, 2012 at 6:39 Matthew Simoneau asked Feb 7, 2011 at 17:31 Matthew SimoneauMatthew Simoneau 6,2796 gold badges37 silver badges46 bronze badges 2
  • Please confirm: The error probably is not thrown per se, but simply appears in the console (only when you open the console). – Ateş Göral Commented Feb 7, 2011 at 18:38
  • 2 I am seeing it when the Chrome console is open, or equivalent in other browsers. With the developer tools closed, this error is silent. It's shaping up that a) there's no way to block this and b) I shouldn't worry about it? – Matthew Simoneau Commented Feb 7, 2011 at 19:08
Add a ment  | 

4 Answers 4

Reset to default 11

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./.../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.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.

http://api.jquery./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:

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

The error function fires for me if the path to json.php is incorrect or if the JSON is malformed. For example:

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

What your plaining 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. This has nothing to do with JavaScript and is a more low level error thrown on the HTTP that is caught by the browser.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论