jQuery $. GetJSON 和 window.位置冲突问题

发布于 2024-10-26 04:41:50 字数 1136 浏览 7 评论 0原文

我遇到了一种奇怪的情况,jQuery $.getJSON / $.ajax 调用 被普通 JavaScript window.location = 'http:/ 中断/new.com/' 事件。

  1. 火灾事件

    window.location = 'http://example.com/something-that-takes-a- Couple-seconds';
    
  2. 当上述 URL 正在连接时,但在浏览器实际重绘任何内容之前,请进行 $.getJSON 调用。就我而言,我碰巧通过滚动到页面底部(加载更多结果)来触发此操作。
  3. 不要实际访问 http://example.com/something-that-takes-a- Couple-seconds,而是连接到 JSON URL http://example.com/ajax /getJSONList.php?params=... 并查看纯文本 JSON 结果集。

以前有人遇到过这个问题吗?

我想一个足够简单的解决方案就是在我触发 window.location 更改后立即通过任何必要的方式阻止所有进一步的 javascript 执行?

编辑:您可以在此处查看正在运行的代码:http://myscubadives.com/,但它不会涉水而过特别有趣。

重现:首先,使用右上角的 Facebook 登录按钮。登录 Facebook,并允许访问我的网站。 Facebook 位连接后,将运行以下行:location.replace('/user/facebook'); - 我尝试过location.replace()、location.href、window.location。所有人都有同样的问题。

现在,当“/user/facebook”正在连接时,快速滚动到页面底部。这将触发 getJSON 调用。您将看到 RAW JSON,而不是点击“/user/facebook”。

如果没有人可以通过这种方式帮助我,我将尝试制作一个示例页面,并尽快删除所有其他内容。

I'm having an odd situation where a jQuery $.getJSON / $.ajax call is being interrupted by a plain JavaScript window.location = 'http://new.com/' event.

  1. Fire event

    window.location = 'http://example.com/something-that-takes-a-couple-seconds';
    
  2. While the above URL is connecting, but before the browser actually redraws anything, make a $.getJSON call. In my case, I happen to trigger this by scrolling to the bottom of the page (loads more results).
  3. Instead of actually going to http://example.com/something-that-takes-a-couple-seconds, instead connect to the JSON URL http://example.com/ajax/getJSONList.php?params=... and see the plain text JSON result set.

Anyone have this problem before?

I suppose a simple enough solution is just to block all further javascript execution by any means necessary right after I fire the window.location change?

EDIT: You can see the code in action here: http://myscubadives.com/, but it won't be especially fun to wade through.

To reproduce: First, use the Facebook login button in the top right. Sign in to Facebook, and allow my site access. Once the Facebook bit connects, the following line runs: location.replace('/user/facebook'); - I've tried location.replace(), location.href, window.location. Same issue on all.

Now, while '/user/facebook' is connecting, scroll to the bottom of the page very quickly. This will fire the getJSON call. Instead of hitting '/user/facebook', you will be presented with RAW JSON.

I'll try to make an example page with all the other stuff stripped out ASAP, if no one can help me this way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

·深蓝 2024-11-02 04:41:51

那么,更改 window.location 不应该强制浏览器连接并开始加载新页面,而放弃旧页面及其所有 AJAX 调用吗?

是的,刚刚检查过,更改了 window.location 上的任何属性,但哈希强制文档重新加载。所以我猜浏览器只是删除所有当前的 AJAX 调用来加载新文档,这对于浏览器来说是非常合乎逻辑的。

至于如何解决这个问题,请在设置window.location后禁止所有AJAX调用。

Well, shouldn't changing window.location force the browser to connect and start loading the new page, abandoning the old one with all of its AJAX calls?

Yeah, just checked, changing any property on window.location but hash forces document reload. So I guess browser just drops all the current AJAX calls to load a new document, which would be pretty logical for browser to do.

As to how to fix this, forbid all the AJAX calls after setting window.location.

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