2015-08-28 84 views
0

我正在使用CakePHP框架。我的问题是,当我打印一个PHP函数时,它给了我一个有很多成员的数组,但是当使用与Ajax相同的函数时,响应是空的。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(); 
        } 
       } 
      }); 
     }) 
    }); 

getCur方法返回格式 “代码”=> “名” 的数组。它从阅读不同来源的XML文件中获取货币。当我在响应外打印函数时,它给了我一个正确的结果,但是使用ajax它只是给了我一个空的数组。

虽然有错误发生。

谢谢!

+0

ajax请求是否成功?检查网络标签(铬) –

+0

你试过'echo $ json'? – rafat

+0

@NorlihazmeyGhazali是的,我获得成功。当状态等于“成功”响应是{“状态”:“成功”,“货币”:[],“currencyRates”:[]},如果状态等于“错误”,那么它给我json充满了错误 – FickDace

回答

0

错误是我创建了方法内的实例,所以阿贾克斯认为我没有任何。

感谢您的回复!

0

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

1.3.x: 

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

$this->request->is('ajax'); 

你也可以检查你的铬网络选项卡的任何特定问题。

+0

正如我的帖子上面提到的CakePHP 2.7所以它是2.x.好吧,我已经检查了标签,似乎没有问题,只是一个空的回应: {“状态”:“成功”,“货币”:[],“currencyRates”:[]} – FickDace

+0

@FickDace如果上述是的,你似乎专注于错误的问题。未显示的方法getCurrency和getCurrencyRate返回空数组 - 你应该问和调试为什么是这种情况。 – AD7six

+0

@ AD7six抱歉,错误的措辞。我有种想法,因为当我在php中打印这个函数的时候它可以工作,但是当通过ajax请求使用相同的函数时 - 响应是空的。任何提示可能? 谢谢! – FickDace

0

尝试:

  1. 呼应你的JSON像echo json_encode($json);
  2. 在成功回调,通过分析数据变量:

    success : function(data){ 
        var json = $.parseJSON(data); 
        console.log(json); 
    } 
    

    见里面控制台的数据。

+0

如果这是“修复”,问题是响应的标题 - 这就是应该改变的。尽管OP已经有效地说了几次,但答案是有效的,但是是空的......如果不是这样的话,这是误导。 – AD7six

+0

控制台给出: 对象{status:“success”,currency:Array [0],currencyRates:Array [0]} currency:Array [0] length:0 proto:Array [0] currencyRates:Array [0] status :“成功” – FickDace