eval解析json注意
asp.net 页面:
<script type="text/javascript">
$(function () {
$("#btnAjax").click(function () {
$.get("Default.ashx", { "userName": $("#txtUserName").val(), "comment": $("#txtComment").val() }, CallBack);
});
});
function CallBack(data) {
var d = eval("("+data+")");
var html = "用户名:" + d.userName + "<br/>评论内容:" + d.comment;
$("#divContent").append(html);
}
</script>
<body>
<form id="form1" runat="server">
<div>
<label for="txtUserName">用户名:</label>
<input type="text" id="txtUserName" />
<label for="txtComment">评论:</label>
<input type="text" id="txtComment" />
<input type="button" id="btnAjax" value="提交" />
</div>
<div id="divContent">
</div>
</form>
</body>
服务器端页面:
string userName = context.Request["userName"];
string comment = context.Request["comment"];
context.Response.Write("{userName:'"+userName+"',comment:'"+comment+"'}"); //注意这里的格式