Yii2 302 Redirect jQuery AJAX 请求,重定向整个页面

我有一个动作:


public function actionAjaxLoadBasketSmall() {

    $this->enableCsrfValidation = false;

    return $this->renderPartial('@app/views/basket/_small');

}

一些 JavaScript


function loadBasketSmall() {

    $.ajax({

        method: "POST",

        url: "/basket/ajax-load-basket-small",

        dataType: "html",

        beforeSend: function () {

            $('#basketSmall').addClass('loading');

        },

        complete: function (data) {

            $("#basketSmall").replaceWith(data.responseText);

        }


    });

}

例如从此函数调用:


function handleProductTileButton(e) {

    e.preventDefault();

    var input = $(this);

    $('#basketSmall').addClass('loading');

    $.ajax({

        method: "GET",

        url: "/basket/ajax-add-product",

        dataType: "html",

        data: {

            productId: input.data('product-id'),

            quantity: 1,

        },

        beforeSend: function () {

            input.addClass('loading');

        },

        complete: function (data) {

            $('.top-bar .search-results').html(data.responseText);

            input.removeClass('loading');

            loadBasketSmall();

        }


    });

}

每当 Ajax 调用完成时,整个页面都会被重定向到 /basket/ajax-load-basket-small


这些是响应头。请注意 302 响应代码。

http://img3.mukewang.com/6167c47e0001f2ff08680652.jpg

ibeautiful
浏览 283回答 2
2回答

holdtom

因此,这取决于使服务器发送重定向响应的语言检测。为了解决这个问题,我将此代码添加到我的基本控制器类中public function init() {    parent::init();    $this->initJsLang();}public static function jsDefineLang() {    return "const LANG = '" . \Yii::$app->language ."';";}protected function initJsLang() {    $this->getView()->registerJs(static::jsDefineLang(), \yii\web\View::POS_HEAD);}并相应地更新了我的 JavaScript:function handleProductTileButton(e) {    e.preventDefault();    var input = $(this);    $('#basketSmall').addClass('loading');    $.ajax({        method: "GET",        url: "/" + LANG + "/basket/ajax-add-product",        dataType: "html",        data: {            productId: input.data('product-id'),            quantity: 1,        },        beforeSend: function () {            input.addClass('loading');        },        complete: function (data) {            $('.top-bar .search-results').html(data.responseText);            input.removeClass('loading');            loadBasketSmall();        }    });}function loadBasketSmall() {    $.ajax({        method: "GET",        url: "/" + LANG + "/basket/ajax-load-basket-small",        dataType: "html",        beforeSend: function () {            $('#basketSmall').addClass('loading');        },        complete: function (data) {            $("#basketSmall").replaceWith(data.responseText);        }    });}

UYOU

所以在穆罕默德的建议之后的最终解决方案语言资源.php<?phpnamespace frontend\assets;use yii\web\AssetBundle;/**&nbsp;* Language application asset bundle.&nbsp;*/class LanguageAsset extends AssetBundle{&nbsp; &nbsp; public function init() {&nbsp; &nbsp; &nbsp; &nbsp; parent::init();&nbsp; &nbsp; &nbsp; &nbsp; $view = \Yii::$app->controller->view;&nbsp; &nbsp; &nbsp; &nbsp; $language = \Yii::$app->language;&nbsp; &nbsp; &nbsp; &nbsp; $js = "const LANG='{$language}';";&nbsp; &nbsp; &nbsp; &nbsp; $view->registerJs($js, $view::POS_HEAD);&nbsp; &nbsp; }}在我的布局文件中,我添加了:use frontend\assets\LanguageAsset;LanguageAsset::register($this);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript