jQuery getJSON 请求返回空响应 - 相同域
我一直在寻找对此问题的解释,但一无所获...
我有一个 jQuery getJSON 请求:
$.getJSON("http://localhost:8080/context/json/removeEntity.html", {
contentId : 1,
entIndex : entityIndex
}, onRemoveEntityResponse);
来自 URL:http://localhost:8080/context/entity.html? contentId=2(相同的域和端口)。
通过 Firefox (5) 和 Chrome 中的 firebug 看到的我的响应是空的。同样有趣的是,我在为 JSON 请求提供服务的 Java 代码中放置了一个断点,并且它被命中,但只有在 Firefox 确定请求已完成之后。
我还调试了一些 jQuery,这里是 AJAX 响应处理中涉及的一些变量(jQuery 1.5.2 未缩小):
[Line: 6651] deferred.rejectWith(callbackContext, [ jqXHR, statusText,错误])
jqXHR: { readyState:0, responseText:"", status:0, statusText:"error", ...})
statusText: "error"
error: ""
有什么想法吗?提前致谢。
编辑:
忽略URL请求HTML文件的事实,它不是真正的HTML,我们使用Tiles 与 Spring MVC 提供映射JSP 文件和 Java 控制器。
我的 Java 控制器应返回的 JSON 如下(如果我直接转到 URL,则为):
{"oldEntityIndex":1,"isSuccess":true,"entityWasSaved":false}
I've been searching for a while for an explanation to this issue but getting no where...
I have a jQuery getJSON request:
$.getJSON("http://localhost:8080/context/json/removeEntity.html", {
contentId : 1,
entIndex : entityIndex
}, onRemoveEntityResponse);
Made from the URL: http://localhost:8080/context/entity.html?contentId=2 (same domain and port).
My response as seen via firebug in Firefox (5) and Chrome is empty. What's also interesting is that I have put a break point in my Java code that serves the JSON request and it is hit, but only after Firefox decides the request is complete.
I have also debugged some of the jQuery and here are some of the variables involved in the AJAX response handling (jQuery 1.5.2 un-minified):
[Line: 6651] deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] )
jqXHR: { readyState:0, responseText:"", status:0, statusText:"error", ...})
statusText: "error"
error: ""
Any ideas? Thanks in advance.
Edit:
Ignore the fact that the URL requests an HTML file, it's not really HTML, we use Tiles with Spring MVC to provide mappings to JSP files and Java controllers.
The JSON that should be returned by my Java controller is as follows (and is if I go to the URL directly):
{"oldEntityIndex":1,"isSuccess":true,"entityWasSaved":false}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的愚蠢 - 我编写的一个 jQuery 插件(并包含在答案中 这里)有一个错误。
该插件删除了发送 AJAX 请求的元素的
onclick
属性,并使用 jQuerybind
绑定它,但我忘记了如何处理onclick
处理程序返回结果。因此,我的onclick
处理程序返回false
并阻止浏览器跟踪底层链接被忽略,我猜浏览器随后取消了 AJAX 请求,因为它知道它即将移动到一个新页面。浪费时间!对不起。
My stupidity - a jQuery plugin I had written (and included in an answer here) had a bug in it.
The plugin had removed the
onclick
attribute of the element that sent the AJAX request off and bound it using jQuerybind
, but what I had forgotten to do with theonclick
handler was return the result. Hence myonclick
handler returningfalse
and preventing the browser from following the underlying link was ignored, I guess the browser then cancelled the AJAX request since it knew it was about to move to a new page.Wasted time! Sorry.