如何从CakePHP 2.2控制器返回JSON?

时间:2022-10-21 22:16:50

I'm invoking a controller function:

我正在调用一个控制器函数:

$.get("http://localhost/universityapp/courses/listnames", function(data){
    alert("Data Loaded: " + data);
});

And in my Controller:

在我的控制器中:

public function listnames() {
    $data = Array(
        "name" => "Sergio",
        "age" => 23
    );
    $this->set('test', $data);
    $this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}

And in that View:

在那个视图中:

<?php echo json_encode($asdf); ?>

However, the Action is returning the entire page including the Layout content (header, footer, navigation).

但是,Action将返回整个页面,包括Layout内容(页眉,页脚,导航)。

What am I missing here? How can I return just the JSON data without the Layout content?

我在这里想念的是什么?如何在没有布局内容的情况下返回JSON数据?

4 个解决方案

#1


10  

You need to disable layout like this

你需要像这样禁用布局

$this->layout = null ;

Now your action will become

现在你的行动将成为现实

public function listnames() {
    $this->layout = null ;
    $data = Array(
        "name" => "Sergio",
        "age" => 23
    );
    $this->set('test', $data);
    $this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}

#2


25  

Set autoRender=false and return json_encode($code):-

设置autoRender = false并返回json_encode($ code): -

public function returningJsonData($estado_id){
    $this->autoRender = false;

    return json_encode($this->ModelBla->find('first',array(
        'conditions'=>array('Bla.bla_child_id'=>$estado_id)
    )));
}

#3


7  

Read up about JsonView on the manual.

阅读手册中的JsonView。

#4


1  

You can try any of the following to return json response (I've taken failure case here to return json response) :

您可以尝试以下任何一种方法来返回json响应(我在这里采取了失败案例来返回json响应):

public function action() {
    $this->response->body(json_encode(array(
        'success' => 0,
        'message' => 'Invalid request.'
    )));

    $this->response->send();
    $this->_stop();
}

OR

要么

public function action() {
    $this->layout = false;
    $this->autoRender = false;
    return json_encode(array(
        'success' => 0,
        'message' => 'Invalid request.'
    ));
}

#1


10  

You need to disable layout like this

你需要像这样禁用布局

$this->layout = null ;

Now your action will become

现在你的行动将成为现实

public function listnames() {
    $this->layout = null ;
    $data = Array(
        "name" => "Sergio",
        "age" => 23
    );
    $this->set('test', $data);
    $this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}

#2


25  

Set autoRender=false and return json_encode($code):-

设置autoRender = false并返回json_encode($ code): -

public function returningJsonData($estado_id){
    $this->autoRender = false;

    return json_encode($this->ModelBla->find('first',array(
        'conditions'=>array('Bla.bla_child_id'=>$estado_id)
    )));
}

#3


7  

Read up about JsonView on the manual.

阅读手册中的JsonView。

#4


1  

You can try any of the following to return json response (I've taken failure case here to return json response) :

您可以尝试以下任何一种方法来返回json响应(我在这里采取了失败案例来返回json响应):

public function action() {
    $this->response->body(json_encode(array(
        'success' => 0,
        'message' => 'Invalid request.'
    )));

    $this->response->send();
    $this->_stop();
}

OR

要么

public function action() {
    $this->layout = false;
    $this->autoRender = false;
    return json_encode(array(
        'success' => 0,
        'message' => 'Invalid request.'
    ));
}