Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 and Pjax redirect not working in Internet Explorer?

I am using the framework Yii2 with their implementation of Pjax. The controller code adds a header "X-Pjax-Url" that is successfully passed with the response.

The redirect for Pjax is handled through the pjax.js methods just fine in Chrome, Safari, and Firefox, but fails in IE11.

When I use the included debugger and view the response headers, the header is there and the url is correct, but IE11 isn't reading it.

The response error: enter image description here

The response headers: enter image description here

Any ideas?

** Update 9/7/15

The controller code:

return $this->redirect(['secure/dashboard']);

Here is a link to the docs (yii\web\Controller) on how to use this method which is a shortcut to this (yii\web\Response) method.

Once again, this works in the Chrome, Safari, and Firefox, just not IE11.

I was using this snippet to verify the xhr response, which returns null.

// pjax complete
$(document).on('pjax:complete', function (event, xhr, textStatus, options) {
    alert(JSON.stringify(xhr));
    var url = xhr.getResponseHeader('X-Pjax-Url');
    if (url) {
        window.location = url;
    }
});

In the images above, you can see the header is actually being sent, it just seems as though IE is reading it properly.

The Pjax.js file actually handles the redirect, I was just using the snippet for debugging purposes.

like image 713
Mike Pearson Avatar asked Aug 29 '15 20:08

Mike Pearson


1 Answers

I can verify this error. But i have no clue if this is an IE error or an yii2 error.

The only thing i know that the redirect works if I dont use "return" and render the normal view additionally to the redirect which does the pjax requests. e.g. i tested it in my index action which is displaying a grid with pjax enabled. Then i redirect on a pjax request (here a sort for a column) to the user view. (just as an example)

public function actionIndex()
{
    if (Yii::$app->getRequest()->getIsPjax()) {
        //return $this->redirect(['user/view','id'=>1]); // doesn't forword in IE.
        $this->redirect(['user/view','id'=>1]); // forwards also in IE.
    }

    $searchModel = new UserSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Because the forward works if the normal view is rendered additionally I guess something is missing (most likely some headers). I would open an issue in the yii2 github and ask for support.

This was btw. the issue where the redirect together with pjax was changed https://github.com/yiisoft/yii2/issues/4395

edit: a quick fix would be to set status code 200 instead of default 302

if (Yii::$app->getRequest()->getIsPjax()) {
    return $this->redirect(['user/view','id'=>1], 200); // forwards also in IE.
}
like image 76
BHoft Avatar answered Oct 01 '22 23:10

BHoft