<!doctype HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script src= ". /jquery.js "></script>
<script>
$ (document). Ready (function () {
$.getjson (' Jsondata.json?id ', function (data) {
Iterate through each entry in the JSON
$.each (Data,function (entryindex,entry) {
var html= ' <tr> ';
html+= ' <td> ' +entry[' name ']+ ' </td> ';
html+= ' <td> ' +entry[' sex ']+ ' </td> ';
html+= ' <td> ' +entry[' home ']+ ' </td> ';
html+= ' </tr> ';
$ (' #title '). After (HTML);
});
});
});
</script>
<body>
<table>
<tr id= "title" >
<th> name </th>
<th> Sex </th>
<th> Home Address </th>
</tr>
</table>
</body>
JSON file:
[
{
"Name": "Xujun",
"Sex": "Male",
"Home": "Nanjing"
},
{
"Name": "Jack",
"Sex": "Male",
"Home": "Beijing"
}
]
The $.ajax of this method, inside the datatype: "JSON" set, if the server returns a JSON-formatted string, it is not necessary to call the Eval method to convert to a JSON object
$.ajax ({
Type: "Post",
URL: "ajax.asp tutorial X",
Data: "Index=5",
DataType: "JSON",
Success:function (data) {
alert (data.name);
var person = DATA.D;
For (var p in person) {
}
});
Example Three
function Btnclick1 () {
$.ajax ({
URL: "Json.ashx",
Type: "Post",
Data: {cmd: "Getinfo1"},
Beforesend:loading,
Success:function (data) {
Using the Eval function
var json = eval (data);
$ ("#dd"). empty ();
Because the list is set above
for (var i = 0; i < json.length; i++) {
$ ("#dd"). Append ("Alert (json[i].id + "name:" + json[i].name);
}
}
});
}
<div>
<input type= "text" name= "Text1" id= "Text1"/><br/>
<input type= "text" name= "Text2" id= "Text2"/>
<br/>
<input type= "button" id= "BTN1" value= "Event 1" onclick= "Btnclick ()"/>
<input type= "button" id= "Button1" value= "Event 2" onclick= "Btnclick1 ()"/>
</div>
<div id= "DD" >
Sd
</div>