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.

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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计