jQuery pjax请求发送两次 [英] jquery pjax request sending twice

查看:269
本文介绍了jQuery pjax请求发送两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

推荐答案

JQuery pjax插件具有默认错误处理程序,该错误处理程序将仅重新加载目标页面.超时过去后,将调用此错误处理程序,而pjax设置得很低.结果,如果您的请求花费的时间太长,您将看到两个相同的请求. pjax请求(可能设置了_pjax属性),然后是另一个非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.

在我遇到的情况中发现的一件事是,响应本身并不需要花费那么长时间.但是,返回的HTML包含嵌入的Flash.我不确定在加载Flash嵌入之前还是之后,pjax代码是否会得到响应.

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.

为解决此问题,我将PJax代码更改为...

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
      });

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

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

这篇关于jQuery pjax请求发送两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆