dqhdpppm02183 2015-08-28 07:44
浏览 76
已采纳

Php函数返回是一个使用Ajax的空数组,否则不是

I'm using CakePHP framework. My problem is that when I print a php function then it gives me an array with a lot of members but when using the same function with Ajax then the response is empty.

Ajax response function:

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

    }

}

View file ajax request:

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

getCur method returns an array in format "code" => "name". It gets the currencies from reading different sources XML files. When I print the function outside of the response it gives me a correct result but with ajax it just gives me an empty array.

Errors are coming through the response though.

Thanks!

  • 写回答

3条回答 默认 最新

  • dongzhan0624 2015-08-28 13:39
    关注

    Mistake was that I created the instances inside the method so ajax thought that I did not have any.

    Thanks for replying!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?