得到错误'在yii2推进

时间:2018-03-16 05:32:15

标签: php yii2

  

异常' yii \ base \ InvalidArgumentException'有消息'响应内容不能是数组。'   在C:\ xampp1 \ htdocs \ advanced \ vendor \ yiisoft \ yii2 \ web \ Response.php:1054

堆栈追踪:

  

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 个答案:

答案 0 :(得分:1)

我得到了解决方案: -

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);
        } 
    }

答案 1 :(得分:0)

您需要更改回复的格式:

您可以通过在组件下的应用程序配置中添加一个数组来修改其配置,如下例所示:

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

Check This Link For More info

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