15

I have tried pjax examples in chrome and firefox, i took the sample code and placed it into my own app but it still does a full page reload. The AJAX request happens then the page moves on without updating the #main div

<html>
    <head>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery-1.8.0.min.js"></script>
        <script src="http://localhost:8888/jul/js/jquery.pjax.js"></script>


         <script type="text/javascript">
            // $(document).ready(function(){
            //   $('a[data-pjax]').pjax();
            // })

        // $(document).ready(function(){
        //     $('a').pjax({

        //    container: '#main'
        //  })
        $('document').ready(function(){
           $('ul a').pjax('#main')
        });


         </script>
    </head>
    <body>
        11:59:36        <div id="main">
             <div class='loader' style='display:none'><img src='http://localhost:8888/jul/imgs/spinner.gif'></div><ul>

  <li><a data-pjax='#main' href="/jul/stats/pjax_stats/index/">Index</a></li>
  <li><a data-pjax='#main' href="/jul/stats/pjax_stats/total_posts/">total_posts</a></li>

  <li><a data-pjax='#main' href="http://localhost:8888/jul/stats/pjax_stats/index">Index</a></li>
  <li><a data-pjax='#main' href="http://localhost:8888/jul/stats/pjax_stats/total_posts">total_posts</a></li>
  <li><a href="http://localhost:8888/jul/stats/pjax_stats/total_graph">total_graph</a></li>
  <li><a href="http://localhost:8888/jul/stats/pjax_stats/twitter_graph">twitter_graph</a></li>
  <li><a href="http://localhost:8888/jul/stats/pjax_stats/facebook_graph">facebook_graph</a></li>
</ul>index files




        </div>

    </body>
</html>

I have tried multiple methods to invoke pjax and maybe someone else could point out where i am going wrong? The Ajax/GET seems to return fine in firebug console- this is an example of my php that produces the pjax response

public function total_posts(){
        // print_r($_SERVER);

        if (!isset($_SERVER["X_PJAX"])) {
            $this->load->view('stats/pjax_stats/header');
            $this->load->view('stats/pjax_stats/links');
        }else{
            echo "pjax";//add in for debug
        }

        echo "total posts";

        if (!isset($_SERVER['X-PJAX'])) {
            $this->load->view('stats/pjax_stats/footer');
        }



    }

A bug?

There seems to be a bug in the latest version where the append variable to the end of the url where the ajax request is made to is _pjax=container instead of _pjax=true

Chris Mccabe
  • 1,902
  • 6
  • 30
  • 61

2 Answers2

36

Have you tried setting the timeout higher (default < 1s )?

For example:

$('document').ready(function(){
  $('ul a').pjax({
    container: '#main',
    timeout: 5000 #ms
  });
});
Laurens
  • 2,420
  • 22
  • 39
0

I thought I would add an answer since I got this problem today and this was the first Google result.

Before you go changing the timeout make sure your container exists. This may sound obvious but if your using a framework, say Yii2 (PHP), you will find that your container ID may no longer exist between refreshes, hence pjax is refreshing the whole page.

So as a note: make sure your container exists before you tweak with timeouts.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • What do you exactly mean by **container**? – Choxx Apr 11 '16 at 09:31
  • @choxx so the PJAX when you go to construct it in many cases attaches to an element with which to load content into, that is the container – Sammaye Apr 11 '16 at 09:42
  • got it. But I think my issue is not related with container. After around 15 minutes of first page load, my page goes refresh in Yii2. Probably it is due to server's late response. – Choxx Apr 11 '16 at 09:44
  • @choxx have you checked dev tools in your browser to see what network parts are saying? – Sammaye Apr 11 '16 at 09:44
  • I tried waiting for it's happening in Networks section in chrome but when I thought of looking at it continuously for the main reason, then browser tricks me. Then it is not going to refresh page!! My bad luck because of some mishappening :( – Choxx Apr 11 '16 at 09:46
  • @choxx that is a tough one indeed hmm – Sammaye Apr 11 '16 at 10:20
  • 1
    I caught tricking of my browser,.. :p Yeah it was due to timeout property of PJAX. The default value is 650 ms. When response tooks more then this, it forces full page reload. I extend this to 5000 as per my requirement and it works like a charm. – Choxx Apr 12 '16 at 10:28
  • @choxx thanks for getting back, I tried timeout on my stuff originally and I should probably have thought about that :) Glad to hear – Sammaye Apr 12 '16 at 10:47
  • Also i think the problem of non existence of container arises when your js loaded before the container's creation. Being on safe side I always call my js/jquery stuff on `$(document).ready(function(){});` and suggest the same to everybody. – Choxx Apr 12 '16 at 12:40
  • 1
    @choxx well, this problem is within the way that pjax handles a non-existent container. Even if you bind the JS to document it will still reload the page since you need an element to assign a pjax view to – Sammaye Apr 12 '16 at 12:42