<script type="text/javascript">
function login() {var data = {};
data.Name = "kk";
data.Age = 12;
$.ajax({
url: "Index",
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(data),
success: function (d) {
alert(d);
},
error: function (d) { alert(d) }
});
}
</script>
<h2>Login</h2>
<div><input type="button" onclick="login()" value="登陆" /></div>
后台接收并返回
public class Person{
public string Name;
public string Age;
}
public JsonResult Index()
{
StreamReader sr = new StreamReader(Request.InputStream);
string posttxt = sr.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
Person contact = js.Deserialize<Person>(posttxt);
JsonResult result = new JsonResult();
result.Data = contact.Name;
return result;
}
前台 接受并弹出 "kk"