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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据