且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery pjax请求发送两次

更新时间:2023-02-05 20:35:17

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.