我正在尝试设置pjax,但是它不起作用。每当我单击pjax链接时,URL都会更改为domain.com/foo
(未更改html内容),然后将其更改为domain.com/#
,然后将其正常重定向到domain.com/foo
。为什么?
这就是我触发pjax的方式: $(document).pjax('a[data-pjax]', '#wrapper');
在我的控制器中,我有这个:
public function getIndex() {
$posts = $this->loginOptions();
$this->layout->title = 'My title';
$content = View::make('foo.bar')
->with('title', $this->layout->title)
->with('posts', []);
if (Request::header('X-PJAX'))
return $content;
else
$this->layout->content = $content;
}
我的HTML(单击链接的第一页)如下所示:
<DOCTYPE html>
<head></head>
<html>
<body>
<div id='wrapper'>
<a data-pjax href='foo'>Foobar</a>
</div>
<script src='http://code.jquery.com/jquery-2.0.0.min.js'></script>
<script src='jquery.pjax.js'></script>
<script src='script.js'></script>
</body>
</html>
如果我使用X-PJAX标头运行标准的ajax调用,我将获得正确的html(这意味着我的If正在运行),但是url并没有改变,这就是为什么我要使用pjax。
$.ajax({
url: '/login',
type: 'get',
beforeSend: function(xhr){
xhr.setRequestHeader('X-PJAX', true);
xhr.setRequestHeader('X-PJAX-Container', '#wrapper')
},
success: function(resp) { $('#wrapper').html(resp); }
})
我放弃了pjax。我现在使用的是history.js。
Javascript:
History.Adapter.bind(window,'statechange',function(){
var State = History.getState();
$.ajax({
url: State.url,
type: 'get',
beforeSend: function(xhr){
xhr.setRequestHeader('X-PJAX', true);
},
success: function(resp, status, xhr) {
document.title = xhr.getResponseHeader('Page-Title');
$('#wrapper').html(resp);
}
});
});
$(document).on('click', 'a[data-pjax]', function(e){
e.preventDefault();
var self = $(this);
History.pushState(null, 'Loading page...', self.attr('href'));
});
PHP(控制器):
public function getIndex() {
$this->layout->title = 'My Title';
$content = View::make('my.view')
->with('title', $this->layout->title)
->with('posts', []);
if (Request::header('X-PJAX')) {
return Response::make($content)
->header('Page-Title', $this->layout->title);
} else
$this->layout->content = $content;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句