Answer the question
In order to leave comments, you need to log in
Yii2 does not parse json requests. How to win?
Colleagues prompt on a question. Send json in ajax request:
var register_headers = new Headers();
register_headers.append('Content-type', 'application/json');
var headers = {headers: register_headers};
return new Promise((resolve) => {
this._http.post('http://localhost/auth/login', JSON.stringify({username: 'username',password: 'password'}), headers)
.subscribe((data) => {if (data.json().success) {...}})
})
Yii::$app->getRequest()->getBodyParams()
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser'
$body = json_decode(Yii::$app->getRequest()->getRawBody(), true);
Answer the question
In order to leave comments, you need to log in
in general, the issue was resolved. It turned out that when cross-domain requests through the browser are sent with json data and the "'Content-type application/json" header, they are converted from post to options.
in Yii I set that the "text/plain" request is perceived as REST, removed the header in js and everything worked.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question