问题是,一旦 pjaxed 请求完成,pjax 也会发起一个正常的 GET 请求。
我的代码是这样的:
$(document).on('pjax:end', function(event){
alert("end");
inpjax = false;
});
$(document).on('pjax:timeout', function(event) {
alert("timeout")
event.preventDefault();
});
$(document).on('pjax:error', function() {
alert("error");
});
$(document).on('pjax:success', function() {
alert("success");
});
$(document).ready(function(e) {
inpjax = false;
$('.pj').click( function(e) {
e.preventDefault();
if(!inpjax)
{
inpjax = true;
$.pjax({
timeout: 5000,
url: $(this).attr('href'),
container: '#codeport'
});
}
});
});
正如你所看到的,它应该在不同的情况下给我一个警报,但我只收到 pjax:end 事件的警报,并且在那个警报之后,pjax 发起正常的 GET 请求,时间是这样的:
[17:36:02.002] GET http://localhost/abstract?_pjax=%23codeport [HTTP/1.1 200 OK 86 ms]
[17:36:02.170] GET http://localhost/abstract [HTTP/1.1 200 OK 73 ms]
我没有收到超时、错误或成功警报。
这可能是什么原因造成的?请帮忙...
解决方案:
问题原来是我的服务器端代码以整页响应,这导致了第二个 GET 请求。因此,如果您也遇到此问题,请确保您的服务器端代码正确响应 PJAX 请求。