浅析ajax请求json数据并用js解析 [转]
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function () { $("#btnget").click(function () { $.ajax({ type: "post", //type:(string)请求方式,POST或GET dataType: "json", //dataType:(string)预期返回的数据类型。xml,html,json,text等 url: "data.ashx", //url:(string)发送请求的地址,可以是服务器页面也可以是WebService动作。 success: function (msg) { var str = ""; for (i in msg) { str += "<tr><td>" + msg[i].id + "</td><td>" + msg[i].name + "</td><td>" + msg[i].cla + "</td><td>" + msg[i].sex + "</td><td>" + msg[i].tel + "</td></tr>"; } $("tbody").append(str); } error: function(){ alert("error"); } }); }); }); </script> </head> <body> <table> <thead> <tr> <td>学号</td> <td>姓名</td> <td>班别</td> <td>性别</td> <td>电话</td> </tr> </thead> <tbody></tbody> </table> <input id="btnget" type="button" value="加载数据" /> </body> </html>
string data = "[{\"id\":\"2010324268\",\"name\":\"林宇\",\"cla\":\"10软件\",\"sex\":\"男\",\"tel\":\"13800138000\"},{\"id\":\"2010324256\",\"name\":\"李四\",\"cla\":\"10网络\",\"sex\":\"女\",\"tel\":\"10010\"},{\"id\":\"2010324264\",\"name\":\"张三\",\"cla\":\"10软件\",\"sex\":\"男\",\"tel\":\"10086\"}]";
context.Response.Write(data);