加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax给webmethod方法传递json数组,C#接收

发布时间:2020-12-16 01:50:37 所属栏目:百科 来源:网络整理
导读:javascript代码段: //!--提交数据-- function btsubmit_click() { var data = "{'record':["; //循环生成data传到后台 $("." + tby + tday).not(":first").each(function () { data = data + "{'id':'" + $(this).prevAll(".itemd").find(".hid_id").val()

javascript代码段:

//<!--提交数据-->
function btsubmit_click() {
var data = "{'record':[";
//循环生成data传到后台
$("." + tby + tday).not(":first").each(function () {
data = data + "{'id':'" + $(this).prevAll(".itemd").find(".hid_id").val() + "','value':'" + $(this).find(".txt" + tday).val() + "'},";
if ($(this).prevAll(".target").text() == "") {
return false;
}
})
data = data.substring(0,data.length - 1) + "]}";
$.ajax({
type: "Post",
url: "Asset.aspx/submits",
data: data,
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (result) {
alert(result.d);
},
error: function (e) {
alert(e.responseText);
}

})
}


C#代码:

[WebMethod]
public static string submits(Dictionary<string,string>[] record)
{
if (record.Length == 0)
{
return "没有任何数据!";
}
else
{
int i = 0;
Dictionary<string,string>[] user = new Dictionary<string,string>[record.Length];
foreach (Dictionary<string,string> stu in record)
{
user[i] = new Dictionary<string,string>();
foreach (string key in stu.Keys)
{
switch (key)
{
case "id":
user[i].Add("id",stu[key]);
break;
case "value":
user[i].Add("value",stu[key]);
break;
default:
break;
}
}
i++;
}

//以下代码循环读取拼接返回页面测试,无实际意义
string ss = "";
foreach (Dictionary<string,string> a in user)
{
ss += ";" + a["id"] + "-" + a["value"];
}
return ss;
}

}

??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读