使用Yii 2在Ajax调用上收到错误请求(#400) [英] Getting bad request (#400) on Ajax calls using Yii 2

查看:75
本文介绍了使用Yii 2在Ajax调用上收到错误请求(#400)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

$(document).on('change', '#tblhotel-int_zone_id', function(e){
    var zoneId = $(this).val();
    var form_data = {
        zone: zoneId
    };
    $.ajax({
        url: "state",
        type: "POST",
        data: form_data,
        success: function(response)
        {
            alert(response);
        }
    });
});

这表明:

错误请求(#400):无法验证您的数据提交.

Bad Request (#400): Unable to verify your data submission.

我已经有<?= Html::csrfMetaTags() ?>.我该如何解决这个问题?

And I already have <?= Html::csrfMetaTags() ?>. How can I fix this problem?

推荐答案

注意:请参阅

Note: See the answer from Skullcrasher to fix the issue in the correct way as my answer suggests disabling the Cross-Site Request Forgery.

您对enableCsrfValidation有问题.要了解更多信息,可以在此处.

You have a problem with enableCsrfValidation. To read more about it you can read here.

要禁用CSRF,请将以下代码添加到您的控制器中:

To disable CSRF, add this code to your controller:

public function beforeAction($action) {
    $this->enableCsrfValidation = false;
    return parent::beforeAction($action);
}

这将对所有操作禁用.您可能应该根据$ action,仅对特定操作禁用它.

This will disable for all actions. You should probably, depending on the $action, disable it only for specific actions.

这篇关于使用Yii 2在Ajax调用上收到错误请求(#400)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆