c# - 将字典从 Ajax 传递到 C# 代码隐藏

标签 c# jquery asp.net ajax

我正在使用 ASP.Net 和 jquery/ajax

我在 jquery 中有以下脚本:

var sizes = [];
sizes.push({ key: "1", value: "3"});
$.ajax({
    type: "POST",
    url: pageUrl,
    data: '{"sizeList":' + sizes + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert("success");
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus); alert("Error: " + errorThrown);
    }
}

这是隐藏代码:

public static void AddProductToDB(Dictionary<String, String> sizeList)

有人可以告诉我我做错了什么,因为我已经尝试了我能想到的一切。 谢谢

最佳答案

var param = {
    '[0].Key': '1', 
    '[0].Value': '3'
};

$.ajax({
    type: "POST",
    url: pageUrl,
    data: param 
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert("success");
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus); alert("Error: " + errorThrown);
    }
}

关于c# - 将字典从 Ajax 传递到 C# 代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20572283/

相关文章:

c# - 安全网络服务器 asp.net

c# - 超过 SMTP 限制发送电子邮件

javascript - 如何 "playback"用户输入的内容 - MVC

c# - 如何在 SendKeys.Send C# 之间创建延迟

c# - Visual Studio 2012 缺少数据源

c# - 如何解析 WordOpenXML 输出中的 mathML?

c# - mvcsitemapprovider - 从不同的页面导航到相同的 ActionResult

jquery - 将悬停效果应用于多个相同元素

jquery - 我的链接变成了特殊字符

javascript - 提交点击时引用 $(this) 到多个输入的问题