我有一個PHP文件,以JSON格式輸出。代碼如下 -JSON輸出沒有顯示
<?php
include 'configure.php';
$qr = "SELECT * FROM student_details";
$res= mysql_query($qr);
$i=0;
while($row = mysql_fetch_array($res))
{
$stud_arr[$i]["full_name"] = $row["full_name"];
$stud_arr[$i]["reg_no"] = $row["regno"];
$stud_arr[$i]["address"] = $row["address"];
$stud_arr[$i]["mark1"] = $row["mark1"];
$stud_arr[$i]["mark2"]= $row["mark2"];
$stud_arr[$i]["mark3"] = $row["mark3"];
$i++;
}
header('Content-type: application/json');
echo json_encode($stud_arr);
?>
此文件時,我的服務器上運行,是給我的結果完全,即所有學生的詳細信息和他們的標誌如下 -
[{"full_name":"Lohith","reg_no":"100","address":"street, lane","mark1":"90","mark2":"87","mark3":"88"},{"full_name":"Ranjeet","reg_no":"101","address":"dfkljg","mark1":"56","mark2":"45","mark3":"39"},{"full_name":"karthik","reg_no":"102","address":"askjldf","mark1":"85","mark2":"90","mark3":"100"}]
現在我想上使用的HTML文件顯示此 -
function getAllDetails()
{
var myTable = '' ;
myTable += '<table id="myTable" cellspacing=0 cellpadding=2 border=1>' ;
myTable += "<tr><td><b>No</b></td><td><b>Full Name</b></td><td><b>Mark1</b></td><td><b>Mark2</b></td><td><b>Mark3</b></td></tr>";var url = "json-example2.php";
$.getJSON(url, function(json) { $.each(json, function(v) {
myTable += "<tr><td>"+v.reg_no+"</td><td>"+v.full_name+"</td><td>"+v.mark1+
"</td><td>"+v.mark2+
"</td><td>"+v.mark3+
"</td></tr>"; });
$("#stud_tbl").html(myTable);});};
上面的代碼被顯示的表,但表示在表中的每個數據單元「未定義」。
No Full Name Mark1 Mark2 Mark3
undefined undefined undefined undefined undefined
undefined undefined undefined undefined undefined
undefined undefined undefined undefined undefined
請幫助如何進行調試。
可能重複[如何返回從AJAX調用的響應?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-打電話) – Bergi 2013-04-22 10:52:30