带有jQuery 1.9和Jersey 2.5.1的JSONP [英] JSONP with Jquery 1.9 and Jersey 2.5.1
问题描述
我在Google上搜索了很多东西,但没有找到任何适合我的需求.我在此处,此处和球衣文档.
I've googled a lot and found nothing that suits my needs. I found these similar threads here, here and here, but they don't solve my problem or I don't understand this correctly. I also read the jersey documentation a couple of times.
我正在使用jersey 2.5.1开发服务器端应用程序,并使用HTML/CSS/JavaScript开发客户端.因此,这很不错,但我没有绊脚石. 我正在使用Media-Moxy作为Java2Json映射器.
I'm developing a server side application with jersey 2.5.1 and the client side with HTML/CSS/JavaScript. So for this worked out great, but no I'm stumbling. I'm using Media-Moxy as my Java2Json mapper.
@GET
@JSONP
@Path("search")
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public String findByTagsAndBoundingBox(@QueryParam("topLeftLat") Double topLat...) {
// do some work
return json.toString();
}
如果我现在在命令行上进行卷曲(请参阅jersey文档的请求,请接受accept标头)
If I now do a curl on the command line (see the accept header as request by jersey documentation)
curl -X GET -H "Accept: application/javascript" "http://localhost:8181/xxxxx/xxxx/search?topLeftLat=59.93704238758132&topLeftLon=10.68643569946289&bottomRightLat=59.890573111743336&bottomRightLon=10.806941986083984&tag=Restaurant&callback=?"
Jersey确实提供了预期的内容(在JSONP中):
Jersey does deliver the content as expected (in JSONP):
callback([{"id":3134,"lon" .... }])
但是如果我这样用jquery调用它:
But if i call this with jquery like this:
$.getJSON("http://localhost:8181/xxxx/yyyy/search?" + query + "&callback=?", function() {
console.log( "success" );
})
我总是会收到错误消息
parsererror, Error: jQuery110207164248435292393_1391195897558 was not called
我可以看到浏览器中的响应包含正确的json,并且得到200返回码.但是由于某种原因,jQuery表示出了问题.
I can see that the response in the browser contains the correct json and I get a 200 return code. But for some reason jQuery says that something is wrong.
我们非常感谢您的帮助, 丹尼尔
Any help is kindly appreciated, Daniel
推荐答案
callback([{
应该已经?([{
使用了与CURL一起使用的URL.
callback([{
should have been ?([{
using the url you used with CURL.
URL中回调参数的目的是指定应执行的回调函数的名称.例如,jQuery指定&callback=jQuery110207164248435292393_1391195897558
应该会导致
The purpose of the callback parameter in the url is to specify the name of the callback function that should be executed. jQuery for example specified &callback=jQuery110207164248435292393_1391195897558
which should result in
jQuery110207164248435292393_1391195897558([{"id":3134,"lon" .... }])
正在从服务中退回.
您需要在服务器代码中更改此行:
You'll need to change this line in your server code:
@JSONP(queryParam = "callback")
ref: https://jersey.java.net/documentation/Latest/user-guide.html#d0e7040
这篇关于带有jQuery 1.9和Jersey 2.5.1的JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!