在Yii 2中发送AJAX发布请求后出现400错误请求 [英] 400 Bad Request after sending AJAX post request in Yii 2
本文介绍了在Yii 2中发送AJAX发布请求后出现400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过ajax将数据发布到这样的特定控制器上:
I want to post data via ajax to some specific controller like this :
<script>
var myUrl = "<?php echo Url::to(['table/edit']); ?>";
</script>
$.ajax({
type : "POST",
url : myUrl,
data: {id: userID},
success : function(response) {
alert("Table is editted");
}
});
在控制器中,我想获取此发布的数据并执行其他操作.但是,当我尝试此操作时,出现错误"400(错误请求)".如果有人可以帮助,那就太好了!谢谢!
And in the controller I want to get this posted data and do some thing else. But when I try this I am getting an error "400 (Bad Request)". If someone can help it would be great! Thanks!
推荐答案
只需添加csrf即可:
Just add csrf like :
<script>
var myUrl = "<?php echo Url::to(['table/edit']); ?>";
</script>
$.ajax({
type : "POST",
url : myUrl,
data: {id: userID,_csrf:'<?=\Yii::$app->request->csrfToken?>'},
success : function(response) {
alert("Table is editted");
}
});
这篇关于在Yii 2中发送AJAX发布请求后出现400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文