jQuery getJSON 请求返回空响应 - 相同域

发布于 2024-11-18 10:46:40 字数 1177 浏览 1 评论 0原文

我一直在寻找对此问题的解释,但一无所获...

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-11-25 10:46:41

我的愚蠢 - 我编写的一个 jQuery 插件(并包含在答案中 这里)有一个错误。

该插件删除了发送 AJAX 请求的元素的 onclick 属性,并使用 jQuery bind 绑定它,但我忘记了如何处理 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 jQuery bind, but what I had forgotten to do with the onclick handler was return the result. Hence my onclick handler returning false 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文