Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jQuery将JSON数据发送到WebMethod返回错误_C#_Jquery_Asp.net_Ajax_Json - Fatal编程技术网

C# 使用jQuery将JSON数据发送到WebMethod返回错误

C# 使用jQuery将JSON数据发送到WebMethod返回错误,c#,jquery,asp.net,ajax,json,C#,Jquery,Asp.net,Ajax,Json,仅当'value'==true时,我才尝试发送一个json列表,其中填充了'data seq'属性中的id 我已经尝试了很多解决方案,但它不断给我错误消息,最常见的是在使用string[]时“类型string没有无参数构造函数”,或者在WebMethod中将string用作代码隐藏参数时“字符串不支持反序列化数组” function sentData() { var json = []; $('.btn[value="true"]').each(function

仅当'value'==true时,我才尝试发送一个json列表,其中填充了'data seq'属性中的id

我已经尝试了很多解决方案,但它不断给我错误消息,最常见的是在使用string[]时“类型string没有无参数构造函数”,或者在WebMethod中将string用作代码隐藏参数时“字符串不支持反序列化数组”

function sentData() {        
    var json = [];
    $('.btn[value="true"]').each(function () {
        var obj = {
            id: $(this).attr("data-seq")
        };
        json.push(obj);
    });
    json = JSON.stringify({ jsonList: json });
    console.log(json); // {"jsonList":[{"id":"38468"},{"id":"42443"},{"id":"42444"}]} (the right id's are getting stored)

    $.ajax({
        type: "POST",
        async: true,
        url: "Default.aspx/getList",
        dataType: "json",
        data: json,
        contentType: "application/json; charset=utf-8",
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
        },
        success: function(json){
            //do something with the result
        }
    });
    return false;
}

// Code-Behind
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static void getList(string jsonList)
{
    // string: string is not supported for deserialization of an array
    // string[]: there is no parameterless constructor for the type string
}

您只是在发送ID,所以将其作为逗号分隔的字符串发送,如下所示:

function sentData() {        
    var json = [];
    $('.btn[value="true"]').each(function () {
        var id = $(this).attr("data-seq")
        json.push(id);
    });

    $.ajax({
        type: "POST",
        async: true,
        url: "Default.aspx/getList",
        dataType: "json",
        data: '{jsonList: "' + json + '" }',
        contentType: "application/json; charset=utf-8",
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
        },
        success: function(json){
            //do something with the result
        }
    });
    return false;
}
和代码隐藏:

[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static void getList(string jsonList)
{
    List<string> ListOfIDs = jsonList.Split(',').ToList();
}
[WebMethod]
[脚本方法(UseHttpGet=false)]
公共静态void getList(字符串jsonList)
{
List ListOfIDs=jsonList.Split(',').ToList();
}

我认为您不应该接受它作为字符串,因为您的结构将是一个
列表
,其中包含某种类型的结构,如
{“jsonList”:[{“id”:“1”},{“id”:“4”},{“id”:“5”},{“id”:“7”}}
。。例如,如果您只是发送ID,为什么不发布字符串列表<代码>[“1”、“2”、“3”]如果我使用列表jsonList,它也不起作用。字符串列表看起来是个好主意,你能举个例子来回答吗?