值不显示在html页面中

时间:2014-04-26 11:49:45

标签: javascript jquery html ajax json

请帮我从外部json文件显示解析的json值。我试图解析javescript中的外部JSON文件,并尝试在html中显示它。但是json文件没有解析。请帮忙。

<html>
<head>
<title>$SiteName: Details </title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2   /jquery.min.js"> </script>
<script src="http://www.json.org/json2.js"></script>
<script>

function test() {

var people = [];
$.getJSON('result_data.json', function(data) {
   $.each(data.result, function(i, f) {
      var tblRow = "<tr>" + "<td>" + i.FName + "</td>" +
       "<td>" + i.LName + "</td>" + "<td>" + i.Email + "</td>" + "<td>" + i.telno + "</td>" + "<td>" + i.msg + "</td>" + "</tr>"
       $('#userdata tbody').append(tblRow);
 });

});

}
</script>
</head>

<body onload="test();">

<div class = "wrapper">
<div class = "profile">
<h3> User feedback </h3>
<hr color = "orange"/>
<table id = "userdata" border="2">
    <thead>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email Address</th>
        <th>telno</th>
        <th>msg</th>
    </thead>
    <tbody>

    </tbody>
</table>

</div>
</div>

</body>
</html>

result_data.json是:

{
    "result": {
        "Email": "xyz@gmail.com",
        "FName": "Harry",
        "Gender": "male",
        "LName": "potter",
        "_id": "535b69928844610ff591be50",
        "msg": "hello",
        "telno": "4567891230"
    }
}

1 个答案:

答案 0 :(得分:1)

$.each的回调中,第一个参数是键或索引,因此i.Email不起作用。第二个参数是值,您还可以使用this来访问该值。

使用data.result[i].Emailf.Email或仅使用this.Email

var tblRow = "<tr>" + "<td>" + this.FName + "</td>" +