将 jQuery ajax 调用中的参数传递给 ASP.NET webmethod

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4244206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:55:33  来源:igfitidea点击:

Passing parameters in a jQuery ajax call to an ASP.NET webmethod

jqueryjsonwebformsasp.net-ajax

提问by larschanders

I know there are more threads about this but they dont help me and I'm going insane here!

我知道有更多关于此的主题,但它们对我没有帮助,我在这里发疯了!

I wanna pass some parameters in to a web method using jQuery Ajax.

我想使用 jQuery Ajax 将一些参数传递给 Web 方法。

var paramList = '';
for(i = 0; i < IDList.length; i++){
    if (paramList.length > 0) paramList += ',';  
        paramList += '"' + 'id' + '":"' + IDList[i].value + '"';  
    }
    paramList = '{' + paramList + '}';  
    var jsonParams = JSON.stringify(paramList);


    $.ajax({
        type: "POST",          
        url: "editactivity.aspx/UpdateSequenceNumber",          
        data: jsonParams,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {

        }
    });

In the ajax call, if I put data to paramList I get the error: "Invalid web service call, missing value for parameter: \u0027a\u0027."

在 ajax 调用中,如果我将数据放入 paramList,我会收到错误消息:“无效的 Web 服务调用,缺少参数值:\u0027a\u0027。”

If I put data to jsonParams I get the error:

如果我将数据放入 jsonParams,我会收到错误消息:

"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027"

“无法将 \u0027System.String\u0027 类型的对象转换为 \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027”

If I write out paramList, it's in a correct JSON format like {"id":"140", "id":"138"}

如果我写出来paramList,它是正确的 JSON 格式,如{"id":"140", "id":"138"}

If I write out jsonParams, it's in an incorrect format like "{\"id\":\"140\",\"id\":\"138\"}"

如果我写出来jsonParams,它的格式不正确,例如"{\"id\":\"140\",\"id\":\"138\"}"

The web method: (it doesn't do that much yet..)

网络方法:(它还没有做那么多......)

[System.Web.Services.WebMethod]
    public static string UpdateSequenceNumber(string a, string b)
    {
         return a+b;
    }

What am I doing wrong? Can't seem to get this JSON thing right.

我究竟做错了什么?似乎无法正确处理这个 JSON 问题。

UPDATE:

更新:

After some help from the first answer I now send {"id":["138","140"]}in the AJAX request.

在第一个答案的帮助下,我现在发送{"id":["138","140"]}AJAX 请求。

The web method now takes a string called idas the parameter instead.

Web 方法现在采用称为id参数的字符串。

[System.Web.Services.WebMethod]
public static string UpdateSequenceNumber(string id)
{
     return id;
}

Now I get the new error:

现在我得到新的错误:

"Type \u0027System.Array\u0027 is not supported for deserialization of an array."

“数组的反序列化不支持类型 \u0027System.Array\u0027。”

回答by Ahmet Kak?c?

Your json parameter names must be same with the c# paramter names.

您的 json 参数名称必须与 c# 参数名称相同。

{"a":"140", "b":"138"}

If you are sending unknown number of parameters to server, you may concat at client-side into one parameter and then split at server-side.

如果您向服务器发送未知数量的参数,您可以在客户端连接成一个参数,然后在服务器端拆分。