问题
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.
回答1:
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
回答2:
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 + "\"})";
}
}
来源:https://stackoverflow.com/questions/15645966/why-doesnt-this-work-jsonp-and-rest-easy