javascript - PJAX not working when requesting PHP file - Stack Overflow

I'm trying to write a site with content loaded into a div at runtime using PJAX. The PHP files tha

I'm trying to write a site with content loaded into a div at runtime using PJAX. The PHP files that i request with the PJAX all include a header and footer, which only produce content if isset($_SERVER['X-PJAX'])

However, when i use the PJAX to load the PHP file, the entire page refreshes. I would assume it's something wrong with my PJAX code, but loading a plain HTML file works fine.

EDIT: In case it is of help, i'm currently running all this on WAMP Server 2.2 running on localhost. It's what i do all my dev stuff in.

for reference, here's the JS i'm using to load the content, and an example of a php page

JS:

if ( $.support.pjax ) {
    //disable <a> tabs, store value of href so pjax can use it
    $('header a').each(function () {
        $(this)
            .attr('pjax', $(this).attr('href'))
            .removeAttr('href')
            .css('cursor','pointer')
    })

    //link up the pjax
    .live('click', function () {
        var link = $(this)
        $('#content').animate({opacity:0}, {duration:500, plete:function(){
            $.pjax({
                url: link.attr('pjax'),
                container: '#content',
                plete: function() {
                    $('#content').animate({opacity:1}, 500);
                }
            });
        }});
    });
}

PHP:

//header.php
<?php
//If we are loading with pjax, ignore the header
if (!isset($_SERVER['X-PJAX'])) {
?>
<!-- head section, opening body tags, etc -->
<div role="main" id="content">
<?php } ?>

//foo.php
<?php include 'header.php'; ?>
<p>sample content</p>
<?php include 'footer.php'; ?>

//footer.php
<?php
if (!isset($_SERVER['X-PJAX'])) {
?>
</div>
<!-- load scripts, etc-->
<?php } ?>

I'm trying to write a site with content loaded into a div at runtime using PJAX. The PHP files that i request with the PJAX all include a header and footer, which only produce content if isset($_SERVER['X-PJAX'])

However, when i use the PJAX to load the PHP file, the entire page refreshes. I would assume it's something wrong with my PJAX code, but loading a plain HTML file works fine.

EDIT: In case it is of help, i'm currently running all this on WAMP Server 2.2 running on localhost. It's what i do all my dev stuff in.

for reference, here's the JS i'm using to load the content, and an example of a php page

JS:

if ( $.support.pjax ) {
    //disable <a> tabs, store value of href so pjax can use it
    $('header a').each(function () {
        $(this)
            .attr('pjax', $(this).attr('href'))
            .removeAttr('href')
            .css('cursor','pointer')
    })

    //link up the pjax
    .live('click', function () {
        var link = $(this)
        $('#content').animate({opacity:0}, {duration:500, plete:function(){
            $.pjax({
                url: link.attr('pjax'),
                container: '#content',
                plete: function() {
                    $('#content').animate({opacity:1}, 500);
                }
            });
        }});
    });
}

PHP:

//header.php
<?php
//If we are loading with pjax, ignore the header
if (!isset($_SERVER['X-PJAX'])) {
?>
<!-- head section, opening body tags, etc -->
<div role="main" id="content">
<?php } ?>

//foo.php
<?php include 'header.php'; ?>
<p>sample content</p>
<?php include 'footer.php'; ?>

//footer.php
<?php
if (!isset($_SERVER['X-PJAX'])) {
?>
</div>
<!-- load scripts, etc-->
<?php } ?>
Share Improve this question edited Feb 27, 2012 at 4:00 AClockWorkLemon asked Feb 27, 2012 at 3:21 AClockWorkLemonAClockWorkLemon 112 silver badges3 bronze badges 7
  • Did you check browser's error console for messages? – Cheery Commented Feb 27, 2012 at 3:38
  • Yeah, nothing es up at all. – AClockWorkLemon Commented Feb 27, 2012 at 3:40
  • I'm getting the feeling that it is something to do with PHP. Almost like the PHP daemon is noticing a request to a PHP file and just spamming the whole file out. But i could be very wrong. – AClockWorkLemon Commented Feb 27, 2012 at 3:59
  • Look at the requests made to the server and its answers (almost every browser can show it). PHP should not create this kind of behavior. The best way is to give a link to a page with a problem. – Cheery Commented Feb 27, 2012 at 4:05
  • Ok. I officially feel stupid. It would seem to be an issue with my local server setup or something. On uploading it to my webhost to display the issue it works fine. I suppose now the question is working out how to make said local server work properly -_- – AClockWorkLemon Commented Feb 27, 2012 at 5:35
 |  Show 2 more ments

1 Answer 1

Reset to default 6

i always have more luck with just using

 $_SERVER["HTTP_X_PJAX"]

Here is the github repo i made of a fully functional PJAX php example, for those that need all the source code to look at to get up and running, and may help anyone else out there searching for PJAX php inplementation

https://github./Jrizzi1/pjaxphp

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745546168a4632348.html

相关推荐

  • javascript - PJAX not working when requesting PHP file - Stack Overflow

    I'm trying to write a site with content loaded into a div at runtime using PJAX. The PHP files tha

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信