how to Json encode in yii2?

后端 未结 3 1258
时光说笑
时光说笑 2021-01-18 03:36

Attempting to encode json and receive 400: Bad Request in yii2. I am trying to encode in Rest client but it is not working properly.



        
3条回答
  •  悲&欢浪女
    2021-01-18 04:40

    i think 400 have nothing to do with json_encode

    google "yii2 csrf" for more information.

    public function actionRegister()
    {
        // is not safe
        Yii::$app->controller->enableCsrfValidation = false;
    
        // set response header
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
      
        $model = new TblUserRegistration();
        $username = $_POST['username'];
        return $username;
    }

    or add scrf in view: form:

    meta:

提交回复
热议问题