我用谷歌搜索了很多,但没有找到适合我需求的东西。我发现了这些类似的线程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 说有些问题。
如有任何帮助,我们将不胜感激,
丹尼尔