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"
无法转换\u0027系统的对象。字符串\ 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..)
web方法:(它还没有做那么多…)
[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.
在第一个答案的帮助下,我现在在AJAX请求中发送{"id":["138","140"}。
The web method now takes a string called id
as 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。阵列\u0027不支持对阵列进行反序列化。
1 个解决方案
#1
15
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.
如果要向服务器发送未知数量的参数,可以将客户端转换为一个参数,然后在服务器端进行分割。
#1
15
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.
如果要向服务器发送未知数量的参数,可以将客户端转换为一个参数,然后在服务器端进行分割。