为什么这不起作用 - jsonp和REST Easy? [英] Why doesn't this work - jsonp and REST Easy?
问题描述
JS代码
<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简易方法
@GET
@POST
@Path("/test/{param}")
@Produces({MediaType.APPLICATION_JSON })
public String returnMessage(@PathParam("param") String msg) {
System.out.println("~~~~~~~~~~~~~"+msg+"~~~~~~~~~~~~");
return "HEllo "+msg;
}
我看到服务器接到电话但浏览器失败了Uncaught SyntaxError:Unexpected Identifier
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.
推荐答案
Resteasy 声称支持JSONP 开箱即用的3.x版本:
Resteasy claims to support JSONP out of the box in 3.x version:
如果您正在使用Jackson,Resteasy有JSONP,您可以通过
添加提供商
org.jboss.resteasy.plugins.providers.jackson .JacksonJsonpInterceptor
(Jackson2JsonpInterceptor,如果你使用的是Jackson2提供商)到
你的部署。如果响应的媒体类型是json并且给出了
回调查询参数,则响应将是一个javascript
片段,其中包含由回调
参数定义的方法的方法调用。例如:
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将产生
响应:
GET /resources/stuff?callback=processStuffResponse will produce this response:
processStuffResponse()这支持jQuery的默认
行为。
processStuffResponse() This supports the default behavior of jQuery.
您可以通过设置
callbackQueryParameter属性来更改回调参数的名称。
You can change the name of the callback parameter by setting the callbackQueryParameter property.
然而,由于 RESTEASY-1168:Jackson2JsonpInterceptor不呈现结束括号
所以
foo ({foo:bar}
渲染而不是
foo({foo:bar})
导致未捕获的SyntaxError:意外的标识符错误
And that causes "Uncaught SyntaxError: Unexpected Identifier" error
我已经明确了< a href =https://github.com/resteasy/Resteasy/pull/638\"rel =nofollow> pull-request 带有修复,希望它应该进入下一个版本3.0.12
I have submimtted a pull-request with a fix and hopefully it should get into next release 3.0.12
我知道这个qustion很老了,但是当你搜索resteasy jsonp问题时它显示在Google的第一页上,所以我决定更新它
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
这篇关于为什么这不起作用 - jsonp和REST Easy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!