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

Solution to 400 error in yii2

尚
Original
2020-03-11 14:31:042584browse

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;
}

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

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

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
});

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!

Statement:
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