【问题标题】:Cakephp 3 Ajax Request Response from controller actionCakephp 3 Ajax 请求响应来自控制器动作
【发布时间】:2016-09-10 16:54:08
【问题描述】:

我试图让 Cakephp 3 在我调用它时向我的 javascript 函数发送一个 json 回复,但我不知道正确的方法..

JS函数:

function add(){
        //serialize form data 
        var formData = $('#newquestion').serialize(); 
        //get form action 
        var formUrl = $(this).attr('action'); 
        $.ajax({ 
            type: 'POST', 
            url: formUrl,
            data: formData, 
            success: function(status){                 
                    console.log('content='+status);  
            }, 
            error: function(xhr,textStatus,error){ 
                alert(error); 

        } });  

}

CakePHP 3 操作:

public function addajax(){      

        if ($this->request->is('ajax')) {       

            $status['msg']="this is a message from cake controller";                
            $this->response->body(json_encode($status));

            return $this->response;

        }
}

问题: 上面的 cakephp 操作有效并发送了正确的输出,但它是否正确; 我不应该使用 ajax 视图或 ajax 布局吗?

当我按如下方式使用序列化时,它不起作用,在 javascript 函数中显示为“未定义”。我究竟做错了什么?正确的做法是什么?

下面的例子也不对吗?

public function addajax(){      

        if ($this->request->is('ajax')) {       
            //$this->viewBuilder()->layout('ajax');         
            //$this->autoRender = false; // Set Render False    


            $status['msg']="this is a message from cake controller";            

            $this->set(compact('status'));
            $this->set('_serialize', ['status']);


            //$this->response->body(json_encode($status));
            //return $this->response;

        }
    }

PS:我已启用 JSON 和 XML 路由和视图。

【问题讨论】:

  • 您的操作方法是正确的,只是不要忘记在方法的开头添加:$this->autoRender=false; .但是我不知道这是在 cakephp 3 中使用 ajax 的正确方法。这篇文章可能会有所帮助:dereuromark.de/2014/01/09/ajax-and-cakephp
  • @user3078643 建议的文章适用于 cakephp 2.0 而不是 3.0。
  • @user221931 有一段关于 cakephp 3.x

标签: jquery ajax cakephp cakephp-3.0


【解决方案1】:

你将通过json发送php数组,然后你在php中编码一个数组并在js函数中解析响应

在你的 CakePhp 3 动作中试试这个:

public function addajax(){ 

    $this -> autoRender = false;     

    if ($this -> request -> is('ajax')) {       

        $status['msg']= "this is a message from cake controller";            

        return json_encode($status);

    }
}

JS函数:

function add(){
    //serialize form data 
    var formData = $('#newquestion').serialize(); 
    //get form action 
    var formUrl = $(this).attr('action'); 
    $.ajax({ 
        type: 'POST', 
        url: formUrl,
        data: formData, 
        success: function(status){  
            var json = JSON.parse(status);             
            console.log('content='+json.msg);  // .msg is name of your index $status['msg']
        }, 
        error: function(xhr,textStatus,error){ 
            alert(error); 
        } 
    });  
}

【讨论】:

  • CakePHP 不仅附带自动请求处理和 JSON 序列化,而且请求对象还包含用于定义响应(正文、标头等)的适当方法,这是 OP 在第一个例子。您的示例通过回避所有这些而使事情变得更糟,并导致异常,因为控制器操作只允许返回null 或\Cake\Network\Response 的实例。另请注意,autoRender 选项在返回响应对象时无效。
  • 好的,那么正确的解决方案是什么@ndm 在我的php函数中添加这个$this->set(compact('status')); $this->set('_serialize', ['status']);?
【解决方案2】:
$this->response->withType('application/json')->withStringBody(json_encode($format));

$format 是你的数组。

【讨论】:

  • 我不知道为什么这被否决了,因为这是正确的答案。
猜你喜欢
  • 2013-11-29
  • 2017-12-07
  • 2020-08-06
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多