Home > PHP Framework > YII > Solution to 400 error in yii2

Solution to 400 error in yii2

Release: 2020-03-11 14:31:04
Original
2622 people have browsed it

Solution to 400 error in yii2

400 error solution in yii2:

The first solution is to close Csrf

Close it in the controller

public function init(){  
  $this->enableCsrfValidation = false;
}
Copy after login

The second solution is to add a hidden field to the form

<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
Copy after login

The third solution is to add the _csrf field to AJAX

var csrfToken = $(&#39;meta[name="csrf-token"]&#39;).attr("content");
$.ajax({
  type: &#39;POST&#39;,
  url: url,
  data: {_csrf:csrfToken},
  success: success,
  dataType: dataType
});
Copy after login

Related tutorial recommendations: yii framework

The above is the detailed content of Solution to 400 error in yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template