Reputation: 1496
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();
}
Upvotes: 0
Views: 631
Reputation: 1496
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);
}
}
Upvotes: 1
Reputation: 3079
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',
// ...
]
OR
function get($id)
$result = user::find()->where(['id' => $id])->all();
return Json::encode($result);
}
Upvotes: 0