我发现可以正确设置pjax,但如果在载荷中发送
<html>
标签,则会触发重新加载 - 也许当您发出请求时,这就是为什么您的页面正在加载的原因?
另外,对于向服务器发送的失败请求,默认超时时间设置为约600毫秒,因此在下面的代码中,我将默认值提高到5000毫秒。
您需要通过http header
"X-PJAX"
来区分服务器端请求是部分文档还是完整文档,而pjax会将其附加到header中。
以下是我使用nodejs / express 5 / pjax解决此问题的解决方案
创建一个名为
pjax-helper.js
的辅助文件,并填入以下内容。
'use strict';
module.exports = function() {
return function pjax( req, res, next ) {
if( req.header( 'X-PJAX' ) ) {
req.pjax = true;
res.locals.pjax = true;
} else {
req.pjax = false;
res.locals.pjax = false;
}
next();
};
};
在您的应用程序中,您可以要求该文件
var pjax = require('./include/pjax-helper');
一旦Express已加载...
app.use(pjax());
现在,您将在应用程序和视图层中都可以使用该变量
在我的情况下,使用EJS模板引擎,我做了这样的事情...
//schedule.ejs:
<%- include('includes/header') %>
<h1><%= title %></h1>
<%- include('includes/footer') %>
--
//includes/header.ejs
<% if(locals.pjax==false) { %>
<!DOCTYPE html>
<html lang="en">
<head> ... </head>
<body>
<%- include('menu') %>
<div id="pjax-container">
<% } %>
--
<% if(locals.pjax==false) { %>
</div><!--/#pjax-ccontainer-->
<script src="/js/pjax.js"></script>
<script>
$(document).pjax('a.pjax', '#pjax-container', {timeout: 5000});
$(document).on('pjax:send', function() { $('#loading').show(); });
$(document).on('pjax:complete', function() { $('#loading').fadeOut(); });
</script>
</body>
</html>
<% } %>
router.get('/schedule', ...);
? - Kino