2011-03-09 70 views
2

我是一個新手,我堅持檢索JSON數據。無法檢索JSON數據使用jQuery

以下是我的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數據庫。 當我打開本地主機/ data.php JSON數據顯示在瀏覽器上。 但如果我打開本地主機/ index.php我不能得到任何所需的輸出。 請解釋。 謝謝!

+1

什麼'json_encode($ jsondata)'o本安輸出? – seriousdev 2011-03-09 23:33:50

+0

這是'index.php'的完整代碼嗎?你有'id =「content」'元素嗎? – Xint0 2011-03-09 23:36:24

+0

**未捕獲的ReferenceError:清單沒有定義**這是我在chrome中得到的錯誤_on按ctrl + shift + i_當我執行data.php時,但是firebug在firefox中並沒有顯示這種類型的東西。 – whatf 2011-03-09 23:50:40

回答

0

嘿, ü沒有關閉腳本標籤正確

嘗試

<script type="text/javascript" src="jquery-1.3.2.min_2.js"></script> 
<script type="text/javascript"> 
$("document").ready(function() { 
    $.getJSON("data.php",function(jsondata){ 
     $("#content").html(jsondata[0].content); 
    }); 
}); 
</script> 
+1

也使用'console.log(jsondata)'來調試 – diEcho 2011-03-10 05:08:47

+0

很好的觀察,但我仍然覺得問題在於data.php。 chrome檢查器給出以下錯誤:Uncaught ReferenceError:manifest沒有定義 – whatf 2011-03-10 11:37:22

1

你需要把所有的頭前,任何「迴響在網頁上的

這樣:

header('Content-type: application/json'); 
echo json_encode($jsonserver); 
+0

感謝您的努力,您的建議似乎不工作。 – whatf 2011-03-10 02:44:21