M
M
Mikha Pankratov2015-03-09 17:27:07
Yii
Mikha Pankratov, 2015-03-09 17:27:07

Yii2 how to accept json?

Hello everyone - just started learning yii2
Made a form...
Made a $.ajax() submission

singup = (function () { 
var name = $("#name").val();
    $.ajax({
        type: 'POST',
        url: "site/singup",
        data: name,
        error: function(e) {
          console.log(e);
        }
    });
});

How can I just not catch the variables in the controller - everything is empty%)
tell me how to catch them? Maybe a special rod is needed?
public function actionSingup() {
        if (Yii::$app->request->isAjax) {
//            $m = Yii::$app->getRequest()->getIsAjax();
//            $m = Yii::$app->getRequest()->getIsPost();
//            $m = Yii::$app->getRequest()->getAuthUser();
//            $m = Yii::$app->getRequest()->getQueryParams();
//            $m = Yii::$app->getRequest()->getQueryString();
                $m = Yii::$app->request->post();
//            echo $m.'<br>';
            return var_dump($m);
        }
    }

Thanks in advance and sorry for such a stupid question :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2015-03-09
@frmax

You are not transmitting data correctly, try like this:
Or like this:
data: "name=" + name,

M
Mikha Pankratov, 2015-03-09
@frmax

Thanks!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question