1

JS code

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $.getJSON("http://localhost:8080/gbsshop/rest/auth/test/xyz?callback=?", function (data) {
        alert("52");
    });
    </script>
</head> 
</html>

REST Easy method

@GET
    @POST
    @Path("/test/{param}")
    @Produces({MediaType.APPLICATION_JSON })
    public String returnMessage(@PathParam("param") String msg) {
        System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~");
        return "HEllo "+msg;

    }

I see that the server gets the call but the browser fails with "Uncaught SyntaxError: Unexpected Identifier"

Any help is appreciated. Thanks for the time.

Asif
  • 1,288
  • 1
  • 16
  • 27
  • 2
    since it is a jsonp request the response should of format `({})`, ex: `mycallback({msg: 'my-message'})` – Arun P Johny Mar 27 '13 at 03:59
  • The name of the callback method is available as a requestparameter `callback` – Arun P Johny Mar 27 '13 at 04:00
  • Really? - i thought "someone" takes care of that in the background. I thought you just need to send the json data from server without having to wrap it in callback function. This can make my code look ugly on server. Did I misunderstand your comment or you really mean I should return - callbackName({result:"john"}) - from my returnMessage function? Isn't this untidy? Is there a better way. I am trying your suggestion now. Thanks – Asif Mar 27 '13 at 04:04
  • @Mustafa The callback method is how JSONP works. Do you even need JSONP? If your front-end and REST service are on the same domain, you can just use plain old JSON – Phil Mar 27 '13 at 04:06
  • @Mustafa I've updated my comments as an answer, As I said I've worked on REST Easy, so I don't know how to do it, my suggestion is a crude way of doing it. – Arun P Johny Mar 27 '13 at 04:10
  • Thanks for getting involved guys. Yes I will need JSONP - I am developing a jQuery mobile application. Apps on your phone and REST code on a server. I am doing above exercise because jQuery mobile was also failing so I decided to do this in simple jQuery first. I don't want to dirty the server code (sorry if you don't like it). What are my options? Should I use Origin headers instead? – Asif Mar 27 '13 at 04:13
  • 1
    possible duplicate of [How enable JSONP in RESTEasy?](http://stackoverflow.com/questions/5350924/how-enable-jsonp-in-resteasy) – Phil Mar 27 '13 at 04:14

2 Answers2

2

Resteasy claims to support JSONP out of the box in 3.x version:

If you're using Jackson, Resteasy has JSONP that you can turn on by adding the provider org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor (Jackson2JsonpInterceptor if you're using the Jackson2 provider) to your deployments. If the media type of the response is json and a callback query parameter is given, the response will be a javascript snippet with a method call of the method defined by the callback parameter. For example:

GET /resources/stuff?callback=processStuffResponse will produce this response:

processStuffResponse() This supports the default behavior of jQuery.

You can change the name of the callback parameter by setting the callbackQueryParameter property.

However it seems that it is borken due to RESTEASY-1168: Jackson2JsonpInterceptor does not render closing bracket

So foo({"foo":"bar"} Is rendered instead of foo({"foo":"bar"})

And that causes "Uncaught SyntaxError: Unexpected Identifier" error

I have submimtted a pull-request with a fix and hopefully it should get into next release 3.0.12

I know that this qustion is pretty old, but it is shown on the first page of Google when you search for resteasy jsonp problems, so I decided to update it

bedrin
  • 4,458
  • 32
  • 53
0

Note: This is the worst possible way to do it in an application, you need to check what framework support might be available. This is just do demonstrate how to add jsonp support

I don't know REST Easy, I'm going to take a big guess here

@GET
@POST
@Path("/test/{param}")
@Produces({MediaType.APPLICATION_JSON })
public String returnMessage(@PathParam("param") String msg, @QueryParam("callback") String callback) {
    System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~");
     return callback + "({msg: \"" + msg + "\"})";
}

Then

$.getJSON("http://localhost:8080/gbsshop/rest/auth/test/xyz?callback=?", function (data) {
    alert(data.msg);
});

In reality you will have to support both json and jsonp requests, so you may need

@GET
@POST
@Path("/test/{param}")
@Produces({MediaType.APPLICATION_JSON })
public String returnMessage(@PathParam("param") String msg, @QueryParam("callback") String callback) {
    System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~");
    if (callback == null || callback == "") {
        return "{msg: \"" + msg + "\"}";
    } else {
        return callback + "({msg: \"" + msg + "\"})";
    }
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • I am trying this Arun. But I would love a much cleaner server side code. Any more suggestions? Thanks. – Asif Mar 27 '13 at 04:14
  • 1
    @Arun I doubt this would work. The problem is the framework will probably attempt to transform the string result using a JSON lib like [Jackson](http://jackson.codehaus.org/). There's better solutions in the duplicate question mentioned above – Phil Mar 27 '13 at 04:17
  • 1
    Phil I m checking that too. Thanks – Asif Mar 27 '13 at 04:19