【发布时间】:2010-12-15 11:30:46
【问题描述】:
我有以下网络服务:
[ScriptService]
public class Handler : WebService {
[WebMethod]
public void method1() {
string json = "{ \"success\": true }";
System.Web.HttpContext.Current.Response.Write(json);
}
[WebMethod]
public object method2(Dictionary<string, object> d) {
Dictionary<string, object> response = new Dictionary<string, object>();
response.Add("success", true);
return response;
}
}
第一种方法接受传统的 html 表单 post 并且响应将 JSON 字符串写入页面。第二种方法接受通过 AJAX 发布的 JSON 值并返回一个序列化对象。
这两种方法都可以自己正常工作,但是当放在同一个 Web 服务中时,调用 method1 时出现此错误:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
当我从 method2 中删除参数时,它们会起作用。
谁能说明为什么会这样?
编辑:
问题涉及方法 2 的参数类型。如果我将其更改为字符串或简单数据类型,它可以正常工作。正如乔尔所说,这可能是因为字典不能被序列化。这似乎不会影响我通过 ajax 发送的请求,只会破坏直接发送到该处理程序的表单。因此,我的解决方法是将表单发布处理程序自己放在单独的文件中。不理想,但适用于我的应用程序。
【问题讨论】:
标签: asp.net web-services