jquery pjax请求发送两次

发布于 2024-12-16 10:40:23 字数 125 浏览 0 评论 0原文

我有一个 Rails 应用程序,但我不断收到 pjax 请求的奇怪行为。当我查看我的开发日志时,我可以看到正在发出两个请求。第一个请求是 pjax,下一个请求不是。结果页面仍然重新加载。

如果有人能帮助我,我将不胜感激。

I have a rails app and I keep getting a weird behavior with pjax requests. When I look into my development logs I could see that two requests are being made. The first request is pjax and the next one is not. As a result the page still reloads.

I appreciate if anyone could help me with this.

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

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

发布评论

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

评论(2

深海夜未眠 2024-12-23 10:40:23

JQuery pjax 插件有一个默认的错误处理程序,它将简单地重新加载目标页面。当超时时间过去时,这个错误处理程序将被调用,pjax 将超时时间设置得非常低。因此,如果您的请求花费的时间太长,您将看到两个相同的请求。 pjax 请求(可能设置了 _pjax 属性),后面跟着另一个非 pjax 请求。在浏览器中,您可能会看到整个页面重新加载。

我在我的情况中发现的一件事是,响应本身并没有花那么长时间。但是,返回的 HTML 包含 Flash 嵌入。我不确定 pjax 代码是否在加载 Flash 嵌入之前或之后获得响应。

为了解决这个问题,我将我的PJax代码更改为如下所示...

$.pjax({
        url: xhr.getResponseHeader('Location'),
        container: '#container',
        timeout: 4000 // pick a suitable timeout
      });

当然,这是直接调用pjax。如果您不直接调用它,则必须找到类似的解决方案。

The JQuery pjax plugin has a default error handler, that will simply reload the target page. This error handler gets called when the timeout has passed, which pjax sets very low. As a result, if your request takes too long, you will see two identical requests. The pjax request (probably with the _pjax attribute set), followed by another non-pjax request. In the browser you will likely see an entire page reload.

One thing I found in my situation, was that the response itself wasn't taking all that long. However, the HTML that was returned included a flash embed. I'm not sure if the pjax code gets its response before or after the flash embed is loaded.

To solve the problem, I changed my PJax code to look like...

$.pjax({
        url: xhr.getResponseHeader('Location'),
        container: '#container',
        timeout: 4000 // pick a suitable timeout
      });

Of course, this is calling pjax directly. If you are not calling it directly, you'll have to find a similar solution.

待"谢繁草 2024-12-23 10:40:23

我也遇到了这个问题 - 看起来这是一个与浏览器缓存相关的问题。我注意到,如果我清除历史记录并缓存,它就会在 Chrome 中停止发生。我还无法解决它,但我想它与禁用浏览器缓存有关?

I've also ran in to this issue - it looks like it is an issue related to browser caching. I've noticed that if I clear the history and cache it stops happening in Chrome. I haven't been able to resolve it yet but I imagine it has something to do with disabling browser caching?

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