0

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!

Ankur
  • 5,086
  • 19
  • 37
  • 62

2 Answers2

2

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); 
    }); 
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thank you for the reply! This request in the future might not be local, so i might still need the callback parameter? – Phillip Atkinson Apr 24 '13 at 12:59
  • then you will have to implement jsonp support in your serverside – Arun P Johny Apr 24 '13 at 13:08
  • @PhillipAtkinson a json response will be like `{ "name" : "some name"}`, but jsonp will be of format `({ "name" : "some name"})` – Arun P Johny Apr 24 '13 at 13:10
  • As a follow up, it works perfectly now. I added a servlet filter to wrap the json response in the callback (as found on another great answer http://stackoverflow.com/questions/5350924/how-enable-jsonp-in-resteasy ). Thanks to everybody who helped! – Phillip Atkinson Apr 25 '13 at 10:16
1

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.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57