我一直在寻找对这个问题的解释,但没有找到......
我有一个 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 未缩小):
[行:6651] deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] )
jqXHR: { readyState:0, responseText:"", status:0, statusText:"error", ...})
statusText: "error"
error: ""
有什么想法吗?提前致谢。
编辑:
忽略URL请求HTML文件的事实,它不是真正的HTML,我们使用Tiles与 Spring MVC提供到 JSP 文件和 Java Controller 的映射。
我的 Java Controller 应返回的 JSON 如下(如果我直接转到 URL,则为):
{"oldEntityIndex":1,"isSuccess":true,"entityWasSaved":false}
最佳答案
我的愚蠢 - 我写的一个 jQuery 插件(并包含在一个答案 here 中)有一个错误。
该插件删除了发送 AJAX 请求的元素的 onclick
属性并使用 jQuery bind
绑定(bind)它,但是我忘记了对 onclick
处理程序返回结果。因此,我的 onclick
处理程序返回 false
并阻止浏览器跟随底层链接被忽略,我猜浏览器随后取消了 AJAX 请求,因为它知道它即将移动到一个新的页面。
浪费时间!对不起。
关于javascript - jQuery getJSON 请求返回空响应 - 同一域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583585/