記錄一下 用法
很基本的JSON 用法 :重點在於Server 丟給 Client的資料格式
與Client 如何取出值
XXX.htm
<html>
<head>
<script type="text/javascript" src="/script/jquery.js"></script>
</head>
<script>
$(document).ready(function()
{
abcd();
});
function abcd()
{
url = "/php_test/json.php"
$.ajax
({
type: "POST",
url: url ,
data:{},
dataType: "json",
beforeSend: function()
{
},
success: function(data)
{
for(var x in data)
{
alert(data[x].name);
alert(data[x].tel);
alert(data[x].address);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
}
});
}
</script>
</html>
json.php =>return value to Client
<?php
$city = array();
$city["0"] = array ("name"=>"aaaa", "tel"=>"a", "address"=> "新竹");
$city["1"] = array ("name"=>"bbbb", "tel"=>"b");
$city["1"]["address"]="桃園";
echo json_encode($city);
?>
留言列表