[b]注意返回时,json的格式最好是"key":"value"的形式,否则有时会取不到返回值。[/b]
ja_PostJson.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font-size: 12px;
}
.comment {
margin-top: 10px;
padding: 10px;
border: 1px solid #ccc;
background: #DDD;
}
.comment h6 {
font-weight: 700;
font-size: 14px;
}
.para {
margin-top: 5px;
text-indent: 2em;
background: #DDD;
}
</style>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript" >
$(function () {
$("#send").click(function () {
$.post("post3.aspx", {
username: $("#username").val(),
content: $("#content").val(),
count:"0"
}, function (data, textStatus) {
debugger;
var j = eval(data);
//var j= eval('(' +data+ ')');
var username =j.username;
var content = j.content;
// alert(content);
//alert(j.count);
if (j != null)
{
$.each(j, function (idx, item)
{
alert(item.username);
alert(item.content);
alert(item.sex);
});
}
},"json");
})
})
</script>
</head>
<body>
<form id="form1">
<p>评论:</p>
<p>
姓名:
<input type="text" name="username" id="username" />
</p>
<p>
内容:
<textarea id="content"></textarea>
</p>
<p>
<input type="button" id="send" value="提交" />
</p>
</form>
<div class='comment'>已有评论:</div>
<div id="resText">
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WDEDUCLoudDEV
{
public partial class post3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
string username = Request["username"];
string content = Request["content"];
string count = "7";
string sex = "male";
//Response.Write("{ \"username\":'" + username + "',\"content\" : '" +content +"',\"count\":'" + count + "'}");
Response.Write("[{ \"username\":'" + username + "',\"content\" : '" + content + "',\"count\":'" + count + "',\"sex\":'" + sex + "'}]");
}
}
}
ja_PostJson.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font-size: 12px;
}
.comment {
margin-top: 10px;
padding: 10px;
border: 1px solid #ccc;
background: #DDD;
}
.comment h6 {
font-weight: 700;
font-size: 14px;
}
.para {
margin-top: 5px;
text-indent: 2em;
background: #DDD;
}
</style>
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript" >
$(function () {
$("#send").click(function () {
$.post("post3.aspx", {
username: $("#username").val(),
content: $("#content").val(),
count:"0"
}, function (data, textStatus) {
debugger;
var j = eval(data);
//var j= eval('(' +data+ ')');
var username =j.username;
var content = j.content;
// alert(content);
//alert(j.count);
if (j != null)
{
$.each(j, function (idx, item)
{
alert(item.username);
alert(item.content);
alert(item.sex);
});
}
},"json");
})
})
</script>
</head>
<body>
<form id="form1">
<p>评论:</p>
<p>
姓名:
<input type="text" name="username" id="username" />
</p>
<p>
内容:
<textarea id="content"></textarea>
</p>
<p>
<input type="button" id="send" value="提交" />
</p>
</form>
<div class='comment'>已有评论:</div>
<div id="resText">
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WDEDUCLoudDEV
{
public partial class post3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
string username = Request["username"];
string content = Request["content"];
string count = "7";
string sex = "male";
//Response.Write("{ \"username\":'" + username + "',\"content\" : '" +content +"',\"count\":'" + count + "'}");
Response.Write("[{ \"username\":'" + username + "',\"content\" : '" + content + "',\"count\":'" + count + "',\"sex\":'" + sex + "'}]");
}
}
}