前台用 JQUERY AJAX POST 传送了一个 JSON 字符串,后台怎么接收这个字符串呢?
代码如下:
$("#btn").click(function(){
var json="{\"data:\"{"
$('#cartProducts .productCart').each(function()
{
json+="[\"cartPName \":"+$('#cartProductName', this).html();
json+=",\"cartQuantity\":"+parseInt($('span.quantity', this).html());
});
json+="}"
$.ajax({
type:"POST",
url:"Default3.aspx",
dataType:"json",
data:json,
success:function(data){}
});
代码如下:
$("#btn").click(function(){
var json="{\"data:\"{"
$('#cartProducts .productCart').each(function()
{
json+="[\"cartPName \":"+$('#cartProductName', this).html();
json+=",\"cartQuantity\":"+parseInt($('span.quantity', this).html());
});
json+="}"
$.ajax({
type:"POST",
url:"Default3.aspx",
dataType:"json",
data:json,
success:function(data){}
});
<%@ Page Language="C#" %><script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (Request.RequestType == "POST")
{
Response.ClearContent();
Response.ContentType = "text/html";
System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream);
string test;
test = reader.ReadToEnd();
Response.Write(test);
reader.Dispose();
Response.End();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script>
function xxx() {
var json = '{"a":"b"}';
$.ajax({
type: "POST",
url: "<%= Request.FilePath %>",
dataType: "html",
data: json,
success: function (result) {
alert("您提交的数据:"+result)
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="btn" type="button" value="button" onclick="xxx()" />
</form>
</body>
</html>
不用可不可以呀.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Line/AddLineMaster/",
data: $.toJSON(TestLineMaster), // 序列化JSON对象,用了一个叫 jquery-json 的插件
dataType: "json",
success: function (json) {
SearchLine();
}
}); [JsonParamFilter(TargetType = typeof(LineMaster), Param = "lineMaster")]
public ActionResult AddLineMaster(LineMaster lineMaster)
{
}
json 可以用过插件转换整这种结构,
下边是 lineMaster在Js段的声明function lineMaster(id, lineName, totalDay, totalCost, feature, lineDescription, compiler, createDate, editDate, stations) {
this.ID = id, //标示
this.LineName = lineName,
this.TotalDay = totalDay,
this.TotalCost = totalCost,
this.Feature = feature,
this.LineDescription = lineDescription,
this.Compiler = compiler,
this.CreateDate = createDate,
this.EditDate = editDate,
this.Stations = stations
}调用方式 var NewLineMaster = new lineMaster("00000000-0000-0000-0000-000000000000", "", 0, 0, "", "",
"00000000-0000-0000-0000-000000000000", NowTime, NowTime, new Array());
reValidator();