2016-07-25 50 views
0

我知道這裏有很多關於這個話題的其他「重複」問題。但我仍然堅持。我只是試圖控制檯通過ajax記錄從PHP傳遞的數組。CakePHP從Ajax郵局撥打電話返回陣列

在CakePHP 2.X:

在View:

<button class="quarter q1" value="1" value>Quarter 1</button> 
<button class="quarter q2" value="2">Quarter 2</button> 
<button class="quarter q3" value="3">Quarter 3</button> 
<button class="quarter q4" value="4">Quarter 4</button> 

<script type="text/javascript"> 
    jQuery(document).ready(function($){ 
    $('.quarter').click(function(e){ 
     e.preventDefault(); 
     var quarter_val = this.value; 

     $.ajax({ 
      url: "/rep/testQueue", 
      type: "post", 
      data: {quarter_val:quarter_val}, 
      success: function(data) { 
       var months = <?php echo json_encode($months); ?>; 

       console.log(months); 
      }, 
      error: function(){ 

      }, 
      complete: function() { 
      } 
     }); 

    }); 
    }); 
</script> 

在我的控制器:

public function queue() { 
    if($this->request->isPost()) { 
    $this->autoRender = false; 
    $this->layout = false; 

    $quarter_chosen = $this->request->data['quarter_val']; 
    $month s= $this->_get_quarter($quarter_chosen); 

    $this->set('months', $months); 
    } 
    } 


    public function _get_quarter($quarter_chosen){ 
     switch($quarter_chosen) { 
      case 1: return array('January', 'February', 'March'); 
      case 2: return array('April', 'May', 'June'); 
      case 3: return array('July', 'August', 'September'); 
      case 4: return array('October', 'November', 'December'); 
     } 
    } 

我試過多種不同的東西。 array_map,JSON.parse,將dataType設置爲json。不過,當我嘗試在ajax成功函數中控制日誌月份時,我得到空值。

如果我沒有正確理解某些內容,請填寫我的信息或分享相關信息。非常感謝你。

+0

哪裏而來PHP變量$個月?爲什麼你沒有在成功函數中使用數據變量? –

+0

您正在從jQuery訪問PHP變量,該變量不知道PHP。您從控制器發送的代碼不是JSON,因此您無法在jQuery中解析它。您無法訪問該頁面中名爲「$ months」的PHP成員,這不是流程。你需要用jQuery來處理它,而不是用PHP。我建議你多閱讀一下jQuery和PHP,不是那麼難,如果你理解得更好,你會變得更好,更容易。 –

+0

它作爲array()出來,但是當我嘗試控制日誌數據時,它什麼也不打印。 –

回答

0

伴侶,您可以使用Cake的JS助手。在Js->請求方法的'成功'上,數據將作爲「數據」接收。

//somewhere on view 
$this->Js->get('.quarter')->event('click', 
    'var quarter_val = $(this).val();' . 
    $this->Js->request(
     array('controller' => 'ya controller', 'action' => 'ya action' , 'ya arguments (if needed)'), 
     array(
      'async' => true, 
      'dataExpression' => true, 
      'data' => '{quarter_val: quarter_val}', 
      'method' => 'POST', 
      'success' => 'console.log(data);' // It should print the returned data into your console 
     ) 
    ) 
); 

//Now, to print the buffered script by the JS helper: 
echo $this->Js->writeBuffer(); 

//To print it into your script block 
$this->append('script'); 
echo $this->Js->writeBuffer(); 
$this->end(); 

你的控制器方法應該使用$ this-> set()來設置數組。您將接收到帶有JS的數據,因此您必須回顯json_encoded數據,以便您的腳本可以使用它。

//On your queue method, instead $this->set('months', $months); 
echo json_encode($months); 
exit(); 
0

你的Ajax請求是:

url: "/rep/testQueue", 

和控制器你沒有任何testQueue()方法。在cakePHP中,您的ajax請求url必須爲:

url: "/yourControllerName/yourMedhodName", 
+0

對不起,我其實只是忘了在這裏正確翻譯。我有testQueue在我實際的代碼,我的意思是取出來的字「測試」這個SO職位。 –