How to get response as json format(application/json) in yii?

后端 未结 8 978
小鲜肉
小鲜肉 2021-01-30 05:08

How to get response as json format(application/json) in yii?

8条回答
  •  抹茶落季
    2021-01-30 05:32

    one more simple way by using

    echo CJSON::encode($result);
    

    example code:

    public function actionSearch(){
        if (Yii::app()->request->isAjaxRequest && isset($_POST['term'])) {
                $models = Model::model()->searchNames($_POST['term']);
                $result = array();
                foreach($models as $m){
                    $result[] = array(
                            'name' => $m->name,
                            'id' => $m->id,
                    );
    
    
                }
                echo CJSON::encode($result);
            }
    }
    

    cheers :)

提交回复
热议问题