我是一个新手,一直卡在检索 json 数据上。
以下是我的index.php:
<script type="text/javascript" src="jquery-1.3.2.min_2.js">
$("document").ready(function() {
$.getJSON("data.php",function(jsondata){
$("#content").html(jsondata[0].content);
});
});
</script>
</head>
<body>
<div id="content"></div>
<div class="lg"></div>
</body>
在我的 data.php 中,我使用标准方式编码和发送数据:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$row = mysql_fetch_row($result);
$jsonserver[$i] = $row;
}
echo json_encode($jsonserver);
header('Content-type: application/json');
mysql_free_result($result);
// close connection
mysql_close($con);
我正在使用mysql数据库。 当我打开 localhost/data.php 时,json 数据显示在浏览器上。 但如果我打开 localhost/index.php 我不会得到任何所需的输出。 请解释。 谢谢!
最佳答案
您需要将所有标题放在页面上的任何“回显”之前
所以:
header('Content-type: application/json');
echo json_encode($jsonserver);
关于php - 无法使用 jquery 检索 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5253620/