2012-04-08 51 views
0

我需要從PHP返回一個數組,並在成功打印時說它未定義。我怎樣才能獲得數組的價值?在jquery中返回數組值

$.ajax({ 
     type: "POST", 
     url: "test2.php", 
     data : {  
        price : [101, 69, 51],   
        id : [1, 2, 3]  
}, 
     success: function(response) { 

     alert response[0]; 
     } 
}) 

// PHP

echo $_REQUEST["price"]; 
+0

是什麼反應? – 2012-04-08 20:03:08

+0

'echo $ _REQUEST [「price」];'可能會導致輸出'Array',因爲這是PHP中數組的默認字符串表示形式。 – 2012-04-08 20:07:09

回答

0

使用echo json_encode(...);對它進行編碼的方式jQuery的理解。然後response就像在你的PHP代碼中一樣。

+0

我已經寫成echo json_encode($ _ REQUEST [「price」]);但沒用。 – user1264362 2012-04-08 20:55:07

0

使用引號:

price : '[101, 69, 51]' 

而在客戶端解析響應JSON:

alert(JSON.parse(response)[0])