猿问

Yii2 302 重定向 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 响应代码。

RISEBY
浏览 298回答 2
2回答

哔哔one

所以穆罕默德建议后的最终解决方案语言资产.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);

白衣染霜花

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