Php函数return是使用Ajax的空数组,否则不会

Php函数return是使用Ajax的空数组,否则不会,php,jquery,ajax,cakephp,Php,Jquery,Ajax,Cakephp,我正在使用CakePHP框架。我的问题是,当我打印一个php函数时,它会给我一个包含很多成员的数组,但是当使用Ajax的相同函数时,响应是空的 Ajax响应函数: public function functionOne(){ $this->autoRender = false; if ($this->request->is('ajax')) { if(!$this->request->data['amount']) {

我正在使用CakePHP框架。我的问题是,当我打印一个php函数时,它会给我一个包含很多成员的数组,但是当使用Ajax的相同函数时,响应是空的

Ajax响应函数:

public function functionOne(){

    $this->autoRender = false;

    if ($this->request->is('ajax')) {
        if(!$this->request->data['amount']) {
            $json['errors'][] = 'Fill in all the required fields';
        }elseif(!is_numeric($this->request->data['amount'])){
            $json['errors'][] = 'Field amound has to be a number';
        }
        if(!$this->request->data['currency_from']){
            $json['errors'][] = 'Fill in all the required fields';
        }
        if(!$this->request->data['currency_to']){
            $json['errors'][] = 'Fill in all the required fields';
        }
        if(!$this->request->data['rate_date']){
            $json['errors'][] = 'Fill in all the required fields';
        }
        if(isset($json['errors'])){
            $json['status'] = 'error';
        }else{
            $json['status'] = 'success';

            $json['curs'] = $this->functionTwo($this->request->data['date1']);
            $json['rates'] = $this->functionThree($this->request->data['date'], $this->request->data['from'], $this->request->data['to'], $this->request->data['amount']);
        }

        print_r(json_encode($json));

    }

}
查看文件ajax请求:

   $(document).ready(function(){
        $('#converter').on('submit', function(e) {
            var form = $(this);
            e.preventDefault();
            $.ajax({
                url: '<?php echo $this->Html->url(array(
                        "controller" => "cur",
                                "action" => "functionOne"
                        ));
                        ?>',
                data: form.serialize(),
                type: 'post',
                async: true,
                success: function (data) {
                    var json = data;
                    if (json.status == 'error') {
                        $('#errors').show();
                        $('#currencies').hide();
                        $('#errors').html('');
                        $.each(json.errors, function (k, v) {
                            $('#errors').append("<span>" + v + "</span>");
                        });
                    }
                    if (json.status == 'success') {
                        var json = data;
                        $('#errors').hide();
                        $('#currencies').show();
                    }
                }
            });
        })
    });
$(文档).ready(函数(){
$('#converter')。在('submit',函数(e)上{
变量形式=$(此);
e、 预防默认值();
$.ajax({
url:“”,
数据:form.serialize(),
键入:“post”,
async:true,
成功:功能(数据){
var json=数据;
如果(json.status=='error'){
$(“#错误”).show();
$(“#货币”).hide();
$('#errors').html('');
$.each(json.errors,函数(k,v){
$('#errors')。追加(“+v+”);
});
}
如果(json.status==“成功”){
var json=数据;
$(“#错误”).hide();
$(“#货币”).show();
}
}
});
})
});
getCur方法以“code”=>“name”格式返回数组。它通过读取不同来源的XML文件来获取货币。当我在响应外部打印函数时,它会给我一个正确的结果,但使用ajax时,它只会给我一个空数组

不过,响应中出现了错误


谢谢

你用的是什么版本的蛋糕??
取决于蛋糕的版本

1.3.x:

$this->RequestHandler->isAjax();
2.x

$this->request->is('ajax');
您还可以查看您的chrome网络选项卡以了解任何特定问题。

尝试:

  • 像echo json一样echo你的json
    echo json\u encode($json)
  • 在成功回调中,使用以下命令解析数据变量:

    success : function(data){
      var json = $.parseJSON(data);
      console.log(json);
    }
    
    请参阅控制台中的数据


  • 错误在于我在方法中创建了实例,所以ajax认为我没有任何实例


    谢谢回复

    ajax请求成功吗?检查网络标签(chrome)您是否尝试“echo$json”?@NorlihazmeyGhazali是的,我成功了。当status等于“success”时,响应为{“status”:“success”,“currency”:[],“currencyRates”:[]},如果status等于“error”,那么它会给我一个充满json的消息errors@rafat是的,上面是我的文章CakePHP2.7上面提到的输出,所以是2.x。我已经检查了选项卡,似乎没有问题,只是一个空的响应:{“状态”:“成功”,“货币”:[],“货币汇率”:[]}@FickDace如果上述是真的,您似乎关注的问题是错误的。Unshawn方法getCurrency和getCurrencyRate返回空数组-您应该询问并调试为什么会出现这种情况。@AD7six对于错误的措辞表示抱歉。我有点不知所措,因为当我用php打印函数时,它可以工作,但是当通过ajax请求使用相同的函数时,响应是空的。有什么建议吗?非常感谢。如果问题是您的取消共享函数,则显示并调试这些函数。这不是顺便说一句。如果这是“修复”问题,那么问题是响应的标题-这就是应该更改的内容。OP有效地说了好几次,虽然回答是有效的,但都是空的。。。如果不是这种情况,则会产生误导。控制台提供:对象{状态:“成功”,货币:数组[0],货币汇率:数组[0]}货币:数组[0]长度:0协议:数组[0]货币汇率:数组[0]状态:“成功”