I've been having this kind of error recently, and I've been looking for the cause. I came across an article that solved it, and I'd like to share it with you.
The first solution is to turn off CSRF
public function init(){
$this->enableCsrfValidation = false;
}
Copy code
The second solution is to add hidden fields to the form
The third solution is to add the _csrf field in AJAX
var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
type: 'POST',
url: url,
data: {_csrf:csrfToken},
success: success,
dataType: dataType
});
Copy code
Yii matching process and Yii::$app->request->csrfToken value storage location description:
Storage location
protected function createCsrfCookie($token)
{
$options = $this->csrfCookie;
$options['name'] = $this->csrfParam;
$options['value'] = $token;
return new Cookie($options);
}
Copy code
Verification method
public function validateCsrfToken($token = null)
{
$method = $this->getMethod();
// only validate CSRF token on non-"safe" methods http://www.w3 .org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1
if (!$this->enableCsrfValidation || in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) {
The above is the entire content of this article, I hope you all like it.
Csrf, POST
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