使用 Yii 2 在 Ajax 调用上收到错误请求(#400)

编程入门 行业动态 更新时间:2024-10-10 13:17:57
本文介绍了使用 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?

推荐答案

注意:参见 Skullcrasher 的答案 以正确的方式解决问题,因为我的答案建议禁用 跨站请求伪造.

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)

本文发布于:2023-10-10 17:52:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Yii   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!