我想從mysql中獲取數據,並用json_encode
來回應它。那麼我的JQUERY
將通過$.getJSON
捕獲JSON
並將結果附加到我的<ul>
。從mysql獲取數據,然後通過JSON顯示
IM試圖找出爲什麼數據被抓獲JQUERY沒有在我的index.php打印
這是我index.php
<html>
<?php
include_once('connect.php');
$sql = "SELECT * FROM data";
$query = mysql_query($sql);
$result = array();
while($row = mysql_fetch_array($query))
array_push($result,
array(
'name' => $row[1],
'msg' => $row[2]));
echo json_encode(array("key" => $result));
?>
<body>
<ul>
//insert fetch data here
</ul>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript" src="./js/custom.js"></script>
</body>
</html>
,這裏是我的JQUERY
function my_function() {
$.getJSON("index.php", function(data) {
$.each(data.key, function(){
$("ul").append("<li>Name: "+this['name']+"</li>"
"<li>message: "+this['msg']+ "</li>");
});
});
}
如果你想有一個PHP腳本來返回JSON,那麼JSON字符串必須是唯一的** **的事情由腳本回聲。你有一堆html會導致無效的json。 –
你在index.php中有srctag嗎?如果是這樣,那就是你的問題,讓它只返回json數組。如果沒有,請將帖子更新爲你有什麼 – Martijn
你可以擴展更多關於你的意思的'必須是腳本' – bobbyjones