Php Ajax请求和回调不起作用无论我尝试了多少次
我有一个简单的PHP代码,它从包含图像的表中检索数据并将其存储在JSON对象中Php Ajax请求和回调不起作用无论我尝试了多少次,php,ajax,json,Php,Ajax,Json,我有一个简单的PHP代码,它从包含图像的表中检索数据并将其存储在JSON对象中 <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } $selected = mysql_select_db("req2",$con) or die("Could not select req2"); $table_first = 'loc
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selected = mysql_select_db("req2",$con)
or die("Could not select req2");
$table_first = 'locationtab';
$query=mysql_query("SELECT Id,City,Image FROM $table_first");
while ($row=mysql_fetch_assoc($query)) {
$array=$row;
$array['Image']=base64_encode($row['Image']);
$output[]=$array;
}
echo json_encode($output);
mysql_close($con);
?>
这非常好,我可以使用print查看JSON对象。
在客户端,我编写了一个Javascript+Ajax代码来调用这个PHP文件,并成功地在JSON对象中显示数据。我不知道下面的代码中缺少了什么,但我似乎根本无法让AJAX部分正常工作。请帮帮我
<html>
<head>
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$.ajax({
url:"table.php",
dataType:'json',
type:'POST',
success:function(output) {
alert( output.Id );
}
});
});
</script>
</head>
<body>
<p>test</p>
</body>
</html>
输出是具有ID的对象数组
您将其视为具有id的单个对象
您需要一个for循环,或者获取一个特定的索引
alert( output[0].Id );