【发布时间】:2011-05-13 18:04:18
【问题描述】:
我知道有更多关于此的线程,但它们对我没有帮助,我在这里要疯了!
我想将一些参数传递给使用 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) {
}
});
在 ajax 调用中,如果我将数据放入 paramList,则会收到错误消息:“无效的 Web 服务调用,参数缺失值:\u0027a\u0027。”
如果我把数据放到 jsonParams 我得到错误:
"无法将 \u0027System.String\u0027 类型的对象转换为类型 \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027"
如果我写出paramList,它是正确的JSON格式,例如{"id":"140", "id":"138"}
如果我写出jsonParams,它的格式不正确,例如"{\"id\":\"140\",\"id\":\"138\"}"
网络方法:(它还没有做那么多..)
[System.Web.Services.WebMethod]
public static string UpdateSequenceNumber(string a, string b)
{
return a+b;
}
我做错了什么?似乎无法正确处理这个 JSON 问题。
更新:
在第一个答案的帮助下,我现在在 AJAX 请求中发送 {"id":["138","140"]}。
Web 方法现在采用名为 id 的字符串作为参数。
[System.Web.Services.WebMethod]
public static string UpdateSequenceNumber(string id)
{
return id;
}
现在我得到了新的错误:
"类型 \u0027System.Array\u0027 不支持反序列化 一个数组。”
【问题讨论】:
标签: jquery json webforms asp.net-ajax