PJax loads new page

I am trying to use pjax to update some html on a page but the content loads in a new page instead of loading asynchronously.

View:


  

<?php Pjax::begin(['enablePushState'=>false]); ?>

<a href="/item/switch?id=1" data-pjax class="btn btn-success">Active</a>

<?php Pjax::end(); ?>



Controller:


    

    public function actionSwitch($id)

    {

        $item = Item::findOne($id);

        if ($item->active)

        {

            $item->active = 0;

            $item->save();


            return $this->render('_inactive_button', [

                'item' => $item

            ]);

            Yii::$app->end();

        }

        else

        {

            $item->active = 1;

            $item->save();


            return $this->render('_active_button', [

                'item' => $item

            ]);

            Yii::$app->end();

        }

    }



So the views rendered contain only the (anchor) button code. When I click the (anchor) button a new page is loaded with the layout and everything else instead of loading the button code via ajax.

Can anyone see what I’m doing wrong?

I fixed it. The access control filter in the controller was breaking it. The access control filter didn’t have anything restricting the request type or anything; it was just checking for a role. I have no idea why this had an effect on the pjax call. For now I have just commented out the access control filter for this particular action and it works fine. If someone knows why the access control filter had this particular effect please chime in.

hi i need some help.

if in main.php i use menu widget inside pjax ,will this load pages without refreshing page

Pjax loads new content between the pjax tags according to what your controller action returns. You should read about pjax I am not sure you understand how it works: https://github.com/defunkt/jquery-pjax