Home >PHP Framework >YII >yii2 post failed (400 error) solution

yii2 post failed (400 error) solution

尚
Original
2019-12-17 13:56:252933browse

yii2 post failed (400 error) solution

yii2 post400 error solution:

The first solution is to turn off Csrf

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

The second solution is to add it to the form Hidden domain

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

The third solution is to add the _csrf field in 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
});

PHP Chinese website, there are a lot of free Yii introductory tutorials, everyone is welcome to learn !

The above is the detailed content of yii2 post failed (400 error) solution. 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
Previous article:How to print sql in yiiNext article:How to print sql in yii