Я знаю, что есть еще темы по этому поводу, но они мне не помогают, и я схожу с ума!
Я хочу передать некоторые параметры в веб-метод с помощью jQuery Ajax.
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) {
}
});
В вызове ajax, если я помещаю данные в paramList, я получаю сообщение об ошибке: «Недопустимый вызов веб-службы, отсутствует значение параметра: \ u0027a \ u0027.»
Если я помещаю данные в jsonParams, я получаю сообщение ошибка:
"Невозможно преобразовать объект типа \ 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"}
If I write out jsonParams
, it's in an incorrect format like "{\"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.
UPDATE:
After some help from the first answer I now send {"id":["138","140"]}
in the AJAX request.
The web method now takes a string called id
as the parameter instead.
[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 массив. "