I
I
Isherath2019-03-07 20:24:08
Yii
Isherath, 2019-03-07 20:24:08

Empty post yii2, what?

What's wrong with this framework?!
I am sending a js array containing several numbers to the server in a Yii2 controller. The result is an empty $_POST. Moreover, the same code works fine on another site.

var xhr = new XMLHttpRequest();
        xhr.open('POST', action, true);
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.send(data);
        xhr.onloadend = function() {
            console.log(xhr.response);
        }

public function actionActivate()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return Yii::$app->request->post();
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Decadal, 2019-03-07
@Decadal

you must specify in the settings

'components' => [
       'request' => [
            'parsers' => [
                'application/json' => \yii\web\JsonParser::class,
            ]
        ],
]

C
coderisimo, 2019-03-07
@coderisimo

_csrf is passed?

V
Vitsliputsli, 2019-03-07
@Vitsliputsli

$_POST gets something that can be parsed, so look in php://input

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question