0

如何在 ASP.NET 中使用 JS post 将数组字符串发送回 Web Method,代码示例如下:

function service() {
            var items = $('#jqxTree').jqxTree('getCheckedItems');
            //  var jsonText = JSON.stringify({ list: items });
            myData = {};
            for (index in items) {
                myData[index] = items[index].label;
            }
            $.ajax({
                type: "POST",
                url: "ServiceConfiguration.aspx/ProcessIndexes",
                data: "{'jsonValue': " + JSON.stringify(myData) + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                cache: false,
                success: function (msg) {

                }
            });
            //alert(items);
        }

现在我正在尝试以下操作:

[WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void ProcessIndexes(List<string> jsonValue)
        {
            var serializer = new JavaScriptSerializer();


        }
4

1 回答 1

1

自从我进入 .NET 领域以来已经有一段时间了,但是这样的事情应该可以工作:

$.ajax({
    type: "POST",
    url: "SomeForm.aspx/SomeWebMethod",
    data: JSON.stringify({ someValues: ["foo", "bar"] }),
    contentType: "application/json",
    dataType: "json",
    success: function(resp) {
        // handle success here
    },
    error: function() {
        // handle error here
    }
});
于 2013-04-01T13:30:59.400 回答