dongyuan1970 2012-04-27 17:16
浏览 94
已采纳

PJAX无法正常工作

My pjax doesn't work, as I see time changes..

Can you find something wrong with my code.

<html><head>
<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script src="jquery.pjax.js"></script>
<script type="text/javascript">
$('a[data-pjax]').pjax()
</script>
</head><body>
<?php
$header = "this is first part
<br/> <a href='pjax2.php' data-pjax='main'>GO</a>";
$date = date("Y-m-d H:i:s");
$footer = "this is second part";
$headers = getallheaders();
if(($headers['X-PJAX'] == 'true')){
echo "<div id='main'>first change</div>";
}else{
echo "$header<br/>$date<br /><div id='main'>first page</div><br />$footer";
}
?>
</body></html>

Best, MJ

  • 写回答

1条回答 默认 最新

  • dongqu9917 2012-04-28 05:10
    关注

    Edit: The main problem is that you are trying to use a cutting-edge browser feature without having mastered basic debugging of a web-page.

    You resolved the error with getallheaders() in php, so this should be easy for you.

    Step 1: Load http://www.bluereservations.com/pjax.php, open the browser console, refresh the page and check for messages in the console.

    It reports an error that $ is not defined. That suggests jquery didn't load successfully.

    Step 2: Load http://www.bluereservations.com/jquery.js

    One mystery solved.

    AFTER getting all the obvious problems with the page out of the way, then look at more obscure things. For pjax that will involve inspection of network resources, http request and response headers, etc.

    If you are using Firebug for Firefox then read http://getfirebug.com/wiki/index.php/Net_Panel for details. For pjax you will need to turn on the persist option in case pjax fails.

    The following might address the other issues with your test-pages...

    When responding to a X-PJAX request you only send the main content, not the whole page. For your test-case something like

    <?php $headers = getallheaders();
          if($headers['X-PJAX'] == 'true'): ?>
    first change
    <?php else: ?>
    <html>
    <head>
    <script src="jquery.js"></script>
    <script src="jquery.cookie.js"></script>
    <script src="jquery.pjax.js"></script>
    <script type="text/javascript">
    $(function() { $('a[data-pjax]').pjax(); });
    </script>
    </head>
    <body>
    <?php
      $header = "this is first part
        <br/> <a href='pjax2.php' data-pjax='main'>GO</a>";
      $date = date("Y-m-d H:i:s");
      $footer = "this is second part";
      echo "$header<br/>$date<br /><div id='main'>first page</div><br />$footer";
    ?>
    </body>
    </html>
    <?php endif; ?>
    

    jquery-pjax does allow you to send the whole file in response to X-PJAX requests. Look in the docs for the fragment option.

    Personally HTMLDecor is a much easier way to get free pushState support, but I'm biased.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集