cakephp jquery ajax返回null

时间:2015-03-01 17:30:12

标签: jquery ajax cakephp

我不确定这里发生了什么或我做错了什么。

当我在chrome开发人员工具中执行console.log()时,来自 cakephp控制器的 ajax响应 e将返回null。 此外,当我检查网络标签时,它表示没有回复。 这是我的代码

view.ctp

var formUrl = "<?php echo Router::url(array('controller' => 'ExamsScores','action' => 'ajaxReturn')); ?>";
        //console.log(form);
        //console.log(exam);
        //console.log(formUrl);
        var string = "form="+form + "&exam="+ exam;
        $.ajax({
            type: "POST",
            url: formUrl,
            data: string,
            dataType: "json",
            success: function(response) {
                console.log(response);
            },
            error: function(xhr,status,error) {
                console.log(status);
            }
        });

controller_action

function ajaxReturn(){
    if($this->RequestHandler->isAjax()){
        $this->autoRender = false;
        $form = $this->request->data['form'];
        $exam = $this->request->data['exam'];
        $results = $this->ExamsScore->query("SELECT concat(students.first_name,' ',students.last_name) as NAME,
            exam_type as EXAM,form_name as FORM,sum(score) as TOTAL,avg(score) as MEAN,exams_scores.admission_no from
            students,exams,forms,exams_scores where (exams_scores.admission_no = students.admission_no) and 
            (exams_scores.exam_id = $exam) and (exams.id = $exam) and (students.form_id = $form) and (forms.id = $form)
            group by exams_scores.admission_no order by TOTAL desc 
            ");
    }
    $jsonData = json_encode($results);
    //print_r($jsonData);
    $this->set('response',$jsonData); 

这是捕捉 ..

如果我评论出控制器中的 print_r($ jsonData),则ajax响应为 null .. 如果我不发表评论,那么回复会回来,因为我预期

究竟导致这种情况发生的原因以及为什么响应为null因为我只是使用print_r进行调试。

任何帮助?

2 个答案:

答案 0 :(得分:1)

好的,有几点需要注意。

重新格式化您的ajax请求,如下所示:

  var url = window.app.url+"/exams/ajaxReturn"; 

  $.ajax({
        type: "POST",
        url: url,
        data: {form:form,exam:exam},
        dataType: "json",
        success: function(response) {
            console.log(response);
        },
        error: function(xhr,status,error) {
            console.log(status);
        }
    });

注意data字段如何更改为与您的格式不同的格式,这将以$_POST['form']$_POST['exam']

的形式到达php文件

现在,你的php文件:

  function ajaxReturn(){

   $this->request->onlyAllow('ajax');
   $this->autoRender = false;
   $this->layout = 'ajax';

   $form = $this->request->data['form'];
   $exam = $this->request->data['exam'];

   // Other code that was omitted for example purposes

   echo json_encode($results);

}

前3行确保它只接受ajax请求,并告诉cakephp不使用该方法呈现任何.ctp文件。

另外,请注意我返回json对象的方式是echo。这是将json返回给ajax的方法。

答案 1 :(得分:0)

它说CakeRequest现在对此负责。您可以找到相应的段落here

function ajaxReturn(){
    if($this->request->is('ajax')){
        $this->autoRender = false;
        $form = $this->request->data['form'];
        $exam = $this->request->data['exam'];
        $results = $this->ExamsScore->query("SELECT concat(students.first_name,' ',students.last_name) as NAME,
            exam_type as EXAM,form_name as FORM,sum(score) as TOTAL,avg(score) as MEAN,exams_scores.admission_no from
            students,exams,forms,exams_scores where (exams_scores.admission_no = students.admission_no) and 
            (exams_scores.exam_id = $exam) and (exams.id = $exam) and (students.form_id = $form) and (forms.id = $form)
            group by exams_scores.admission_no order by TOTAL desc 
            ");
    }
    $jsonData = json_encode($results);
    //print_r($jsonData);
    $this->set('response',$jsonData);