jquery - 使用 HTTPGET 的 C#.NET Web 服务返回 System.IndexOutOfRangeException

标签 jquery ajax web-services json jquery-ui-autocomplete

我已设置此示例 Web 服务来调查错误来源:

    namespace userControlPanel.webservice
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public OutputData1 AjaxGetMore(InputData1 input)
        {
            return new OutputData1()
            {
                id = input.id,
                message = "it's work!",
                myInt = input.myInt + 1
            };
        }

    }
    public class OutputData1
    {
        public string id { get; set; }
        public string message { get; set; }
        public int myInt { get; set; }
    }
    public class InputData1
    {
        public string id { get; set; }
        public int myInt { get; set; }
    }

}

它基于此处的示例

所以我尝试在这里获取结果:

http://localhost:57109/webservice/WebService1.asmx/AjaxGet?id=li1234

我得到了结果,

 System.IndexOutOfRangeException: Index was outside the bounds of the array
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

所以,我认为它是 web.config 对吗?所以我实现了建议的设置(PS,我还创建了一个 HTTP-POST Web 服务,它工作得很好)

来自 web.config:

<webServices>
      <protocols>
        <add name="HttpPost"/>
        <add name="HttpPostLocalhost"/>
        <add name="HttpGet"/>
      </protocols>
    </webServices>

    <httpHandlers>
      <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
    </httpHandlers>
  </system.web>

最佳答案

事实证明,这个问题是我的一些糟糕的代码重构和类/命名空间问题造成的。

另外,想指出的是,在使用 jQuery Web 服务时远离 HTTPGET 是一个好主意,我读过的重新信息 here

关于jquery - 使用 HTTPGET 的 C#.NET Web 服务返回 System.IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3553950/

相关文章:

javascript - 如何在模态 Bootstrap 中发送参数?

c# - WCF 和 Ajax。不允许的方法让我发疯

android - GCM(Google Cloud Messaging)在android编程中是否比Web服务通信有任何优势

javascript - FormData 控制台记录 [object FormData ] 而不是实际文件

jquery数据表服务器端分页mvc与动态列

javascript - 何时以及如何调用寻找页面滚动操作的 JavaScript 函数?

javascript - Ajax 请求当前路径请求 `text/html` 而不是 `application/json`

javascript - json兄弟数据

javascript - 将文件从 Apache 提供给 Javascript

json - 从 ASMX Web 服务返回 JSON,没有 XML 包装器?