JSONP 与 Jquery 1.9 和 Jersey 2.5.1

2024-03-23

我用谷歌搜索了很多,但没有找到适合我需求的东西。我发现了这些类似的线程here https://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type, here https://stackoverflow.com/questions/14621356/error-jquery1830649454693285679-1359620502896-was-not-called-json and here https://stackoverflow.com/questions/19435064/jsonp-json-callback-method-not-called-parsererror,但他们没有解决我的问题或者我没有正确理解这一点。我还读过球衣文档 https://jersey.java.net/documentation/latest/user-guide.html#d0e6913一些时间。

我正在使用 jersey 2.5.1 开发服务器端应用程序,并使用 HTML/CSS/JavaScript 开发客户端应用程序。所以这件事进展顺利,但我没有绊倒。 我使用 Media-Moxy 作为我的 Java2Json 映射器。

@GET
@JSONP
@Path("search")
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public String findByTagsAndBoundingBox(@QueryParam("topLeftLat") Double topLat...) {
    // do some work
    return json.toString();
}

如果我现在在命令行上执行curl(请参阅球衣文档请求的接受标头)

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 中):

callback([{"id":3134,"lon" .... }])

但如果我用 jquery 调用它,如下所示:

$.getJSON("http://localhost:8181/xxxx/yyyy/search?" + query + "&callback=?", function() {
     console.log( "success" );
})

我总是收到错误

 parsererror, Error: jQuery110207164248435292393_1391195897558 was not called 

我可以看到浏览器中的响应包含正确的 json,并且我得到了 200 返回码。但由于某种原因 jQuery 说有些问题。

如有任何帮助,我们将不胜感激, 丹尼尔


callback([{本来应该?([{使用与 CURL 一起使用的 url。

url 中回调参数的作用是指定应执行的回调函数的名称。以 jQuery 为例指定&callback=jQuery110207164248435292393_1391195897558这应该导致

jQuery110207164248435292393_1391195897558([{"id":3134,"lon" .... }])

从服务中返回。

您需要更改服务器代码中的这一行:

@JSONP(queryParam = "callback")

ref: https://jersey.java.net/documentation/latest/user-guide.html#d0e7040 https://jersey.java.net/documentation/latest/user-guide.html#d0e7040

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSONP 与 Jquery 1.9 和 Jersey 2.5.1 的相关文章

随机推荐