<head runat="server">
<title></title>
<script language="javascript" type="text/javascript" src="https://www.jb51.net/Scripts/jquery-1.4.1.min.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$("#btn").click(function () {
$.getJSON("GetJsonDemo.aspx", { Action: "action" },
function (data) {
var txt = "";
$.each(data, function (k, v) { $.each(v, function (m, n) { txt += "id :"+n.id + ";name :" + n.name + ";sex :"+n.sex+"<br/>" }); });
$("#txt").html(txt);
});
});
});
$(function () {
$("#btn2").click(function () {
$.get("GetJsonDemo.aspx", { Action: "action" },
function (data) { $("#txt2").text(data); });
});
});
</script>
</head>
<body>
<form runat="server">
<div>
<input type="button" value="paser json" /><br />
<input type="button" value="watch json string" /><br />
<label></label><br />
<label></label>
</div>
</form>
</body>
后台页面:
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
JsonAjax();
}
private void JsonAjax() {
string action = Request["Action"];
if (!string.IsNullOrEmpty(action) && action == "action") //判断是否通过前台的点击事件进来的
{
string str = DataTableConvertJson.DataTableToJson("json", Data.BindData().Tables[0]);
Response.Write(str);
Response.End();
}
}
最后给大家展示一下生成的json格式:
Top of Form
{"json":[{"id":"16","name":"zhaoliu","sex":"man"},{"id":"19","name":"zhangsan","sex":"women"}]}
Bottomof Form
您可能感兴趣的文章: