試圖檢索的名稱和城市在jquery中。看起來PHP結果在jQuery中作爲數組返回。在jQuery中獲取php數組?
<?php
$val = array(
name => $_POST["name"],
city => $_POST["city"]
);
foreach($val as $res) {
echo $res;
}
?>
$(document).ready(function(){
$.post("get.php",
{
name : "fakename",
city : "fakecity"
},
function(data){
// returns name and city as a single array
alert(data);
// how to get it as an associative array
// desired result :
//* alert(data.name);
//* alert(data.city);
}
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
爲什麼不使用JSON? – Machavity
我嘗試過使用JSON.parse(),但它沒有解決。 – YSuraj