我花了一段时间调试为什么我的 pjax 设置不起作用 - 这是我目前所知道的。 2 个请求在同一秒内发生。这很奇怪,因为我在前端设置了 3 秒的超时。
[02/Nov/2014 05:42:26] "GET /i/feed/?_pjax=%23pjax HTTP/1.1" 200 0
[02/Nov/2014 05:42:26] "GET /i/feed/ HTTP/1.1" 200 62358
在客户端:
$(document).ready(function() {
$(document).pjax('a[data-pjax]', '#pjax',
{'timeout': 3000});
});
在 html 上(存在 pjax 目标):
<div id="pjax" class="cl-mcont">
{% block content %}
{% endblock %}
</div>
此外,永远不会检测到 HTTP header 。我有这个片段:
base_template = ('pjax.html' if request.META.get('HTTP-X-PJAX')
else 'base_v2.html')
base_template 始终是 base_v2.html。
我很困惑为什么 pjax 能够修改 url,并导致发送额外的请求,但同时没有正确设置 header 。
如有任何想法,我们将不胜感激。
最佳答案
我认为 HTTP
header 应该是 HTTP_X_PJAX
(下划线连接而不是破折号)而不是 HTTP-X-PJAX
。作为 django docs状态:
Any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER
对于双重事件(请求)触发,请确保您没有将 a[data-pjax]
选择器与 pjax
绑定(bind)两次。绑定(bind)应该只进行一次。
关于javascript - 无法使用 Django 设置 jquery pjax(未设置 header ,但添加了 GET 参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26696454/