简体   繁体   中英

Why is the fail method being called from my jQuery.getJSON() method?

I have a html page loaded directly from a local file in a web browser (FF 14.0.1 or Chromium, on Ubuntu 12.04)

The html page includes a local jQuery js file, and then includes a local js file with this method:

function start() {
     $.getJSON("http://localhost:8080/app/connect?callback=?", "id=11", function() { alert("win!"); })
     .done(function() { alert("done"); })
     .fail(function(xhr, request, error) { alert(xhr.status + "<> + request + "<>" + error); }); }

I'm responding to these requests from a JBoss / Restful web server with no special configuration, the method on the server is annotated with @Produces({"application/json"}). Server doesn't show any errors, even gets the id value correctly.

When I trigger this javascript, the fail method is called and I get this alert:

200<>parsererror<>jQuery311391951_513134 was not called

I can see the JSON response when inspecting with Firebug, it looks ok. In Chromium, I can inspect the request / response headers and everything looks ok. I have that text saved, if anyone thinks it might give more insight.

Can anyone tell me what's going on? Why do I get this error?

Thanks in advance!

It looks like you are accessing a local resource, in that case you don't have to use jsonp. You can remove the callback=? from the url.

It is used if you have to access a third party resource with Same Origin Policy violations

function start() {
    $.getJSON("http://localhost:8080/app/connect", "id=11", function() { 
        alert("win!");
    }).done(function() { 
        alert("done"); 
    }).fail(function(xhr, request, error) { 
        alert(xhr.status + "<>" + request + "<>" + error); 
    }); 
}

You are using JSONP (JSON with Padding). The server must unterstand the ?callback= Paramater and wrap the JSON answer with the callback function. This is mandatory for calls where SOP restrictions apply. If not (same domain and port) then remove the callback parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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