我想得到JSON
来自 ajax 调用的 php 页面的数据。php 页面正在返回AJAX
字符串,现在我必须得到它JSON
数据和显示值分开。
我怎样才能做到这一点???
这是我正在使用的代码......
当我运行此代码来获取数据product_id 时,它显示警报未定义。
code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request json test</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script language="JavaScript">
$(document).ready(function(){
$("#testjson").click(function(e){
startJsonSession();
return false;
});
function startJsonSession(){
$.ajax({
url: "index.php",
cache: false,
dataType: "json",
complete: function(data) {
pid = data.product_id;
alert(pid);
}
});
}
});
</script>
</head>
<body>
<a href="#" id="testjson">Get JSON Data</a>
<div id="showdata"></div>
</body>
</html>
index.php 的 JSON 输出:
[{"ID":"1","product_id":"3","image_name":"cycle5.png","image_type":".jpg"},{"ID":"2","product_id":"6","image_name":"cycle3.png","image_type":".jpg"},{"ID":"3","product_id":"4","image_name":"cycle2.png","image_type":".jpg"},{"ID":"4","product_id":"43","image_name":"cycle1.png","image_type":".jpg"},{"ID":"5","product_id":"7","image_name":"cycle8.png","image_type":".jpg"},{"ID":"6","product_id":"9","image_name":"cycle0.png","image_type":".jpg"},{"ID":"7","product_id":"543","image_name":"cycle6.png","image_type":".jpg"},{"ID":"8","product_id":"445","image_name":"cycle9.png","image_type":".jpg"},{"ID":"9","product_id":"453","image_name":"cycle75.png","image_type":".jpg"},{"ID":"10","product_id":"725","image_name":"cycle86.png","image_type":".jpg"}]