0
votes

Exception 'yii\base\InvalidArgumentException' with message 'Response content must not be an array.' in C:\xampp1\htdocs\advanced\vendor\yiisoft\yii2\web\Response.php:1054

Stack trace:

0 C:\xampp1\htdocs\advanced\vendor\yiisoft\yii2\web\Response.php(337): yii\web\Response->prepare()

1 C:\xampp1\htdocs\advanced\vendor\yiisoft\yii2\base\Application.php(392): yii\web\Response->send()

2 C:\xampp1\htdocs\advanced\frontend\web\index.php(17): yii\base\Application->run()

3 {main}

SiteController.php

public function actionGetuser()
    {
        $model = new UsersData();
        if(Yii::$app->request->isAjax){
            $id =  Yii::$app->request->post();
              return $model->get($id);
        } 
    }


model:-
function get($id)
    {
        $model = Yii::$app->db->createCommand("SELECT * FROM user where id=$id");
        return  $user = $model->queryOne();
    }
2
Right now, your code is open to sql injection, every request for get user data can do something really really bad.Yupik

2 Answers

1
votes

I got the solution :-

model:-
     function get($id)
        {
            $userid = json_decode($id);
            $uid = $userid->id;
            $model = Yii::$app->db->createCommand("SELECT * FROM user where id = $uid");
            $user = $model->queryOne();
            //return $user;
            return  json_encode($user);
        }

controller:-
 public function actionGetuser()
    {
        $model = new UsersData();
        //return "Dfasdafsd";
        if(Yii::$app->request->isAjax){
            $data =  Yii::$app->request->post();
            $id = json_encode($data);
            return $model->get($id);
        } 
    }
0
votes

You need to change format of your response :

You can modify its configuration by adding an array to your application config under components as it is shown in the following example:

'response' => [
    'format' => yii\web\Response::FORMAT_JSON,
    'charset' => 'UTF-8',
    // ...
]

Check This Link For More info

OR

function get($id)
   $result = user::find()->where(['id' => $id])->all();
   return Json::encode($result);
}