如何获得响应如YII JSON格式(应用/ JSON)?

有帮助吗?

解决方案

对于Yii的1:

创建在(基),该功能控制器:

/**
 * Return data to browser as JSON and end application.
 * @param array $data
 */
protected function renderJSON($data)
{
    header('Content-type: application/json');
    echo CJSON::encode($data);

    foreach (Yii::app()->log->routes as $route) {
        if($route instanceof CWebLogRoute) {
            $route->enabled = false; // disable any weblogroutes
        }
    }
    Yii::app()->end();
}

然后只需在动作结束调用:

$this->renderJSON($yourData);

对于Yii的2:

的Yii 2具有内置的这种功能,使用下面的代码在您的控制器动作的末尾:

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;

其他提示

$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
Yii::app()->end(); 

有关Yii2控制器内:

public function actionSomeAjax() {
    $returnData = ['someData' => 'I am data', 'someAnotherData' => 'I am another data'];

    $response = Yii::$app->response;
    $response->format = \yii\web\Response::FORMAT_JSON;
    $response->data = $returnData;

    return $response;
}
$this->layout=false;
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end(); 
class JsonController extends CController {

    protected $jsonData;

    protected function beforeAction($action) {
        ob_clean(); // clear output buffer to avoid rendering anything else
        header('Content-type: application/json'); // set content type header as json
        return parent::beforeAction($action);
    }

    protected function afterAction($action) {
        parent::afterAction($action);
        exit(json_encode($this->jsonData)); // exit with rendering json data
    }

}

class ApiController extends JsonController {

    public function actionIndex() {
        $this->jsonData = array('test');
    }

}

通过使用一个更简单的方法

echo CJSON::encode($result);

示例代码:

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

欢呼:)

在控制器中的操作与要渲染 JSON 数据,例如:actionJson ()

public function actionJson(){
    $this->layout=false;
    header('Content-type: application/json');
    echo CJSON::encode($data);
    Yii::app()->end(); // equal to die() or exit() function
}

请参阅更 Yii的API

Yii::app()->end()

我觉得这个解决方案是不结束应用程序流的最好方式,因为它使用PHP的exit()功能,女巫指执行流立即退出。是的,有Yii中的onEndRequest处理程序,PHP的register_shutdown_function但它仍然过于宿命。

有关我的更好的方法是本

public function run($actionID) 
{
    try
    {
        return parent::run($actionID);
    }
    catch(FinishOutputException $e)
    {
        return;
    }
}

public function actionHello()
{
    $this->layout=false;
    header('Content-type: application/json');
    echo CJavaScript::jsonEncode($arr);
    throw new FinishOutputException;
}

因此,应用程序流程继续甚至之后执行。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top