Ajax调用.asmx页面方法失败,无效System.InvalidOperationException:缺少参数

时间:2015-06-28 22:32:31

标签: jquery asp.net ajax invalidoperationexception

我正在尝试使用jquery自动完成和ajax调用webservice方法,调用在localhost上工作正常但是当发布到开发服务器时它每次都失败并使用谷歌浏览器调试我得到以下错误:

 System.InvalidOperationException: Missing parameter: sLookUP.
 at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
 at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()    
 at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

我正在使用 ASP.NET3.5 IIS 7.5 ,其中dev计算机 IIS 6 ASP.NET 2.0

我的ajax电话如下:

        $("#<%=txtSearch.ClientID %>").autocomplete({
        source: function(request, response) {
                $.ajax({
                url: "Address.asmx/ShowAddress",
                data: "{ 'sLookUP': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function(data) {

我在Address.asmx中的网络方法:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public String[] ShowAddress(string sLookUP)
    {
        List<string> lstAddresses = new List<string>();

        DataTable dtAddresses;
        Address Addr = new Address();
        dtAddresses = Addr.GetLookupAddresses(sLookUP);


        foreach (DataRow row in dtAddresses.Rows)
        {
            lstAddresses.Add(string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}", row["OrderAddress_name"], row["OrderAddress_id"], row["OrderAddress_1"], row["OrderAddress_2"], row["OrderAddress_town"], row["OrderAddress_county"], row["OrderAddress_postcode"], row["OrderAddress_fulladdress"],row["OrderAddress_ClusterID"]));
        }
        return lstAddresses.ToArray();
    }

web.config设置(有关其他问题的建议应该有所帮助:

<webServices>

      <protocols>
        <add name="HttpSoap"/>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        <add name="Documentation"/>
      </protocols>
    </webServices>




<system.webServer>
    <handlers>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </handlers>
  </system.webServer>

任何建议欢迎我已经尝试了很多东西,但因为它在我的机器上运行我正在考虑它的iis / web.config设置我错过或写错了因为开发机器有不同的设置。感谢

1 个答案:

答案 0 :(得分:0)

您需要将属性[System.Web.Script.Services.ScriptService]添加到a​​smx服务声明中。将asmx添加到解决方案时,Visual Studio会使用此属性创建服务主体,但会进行注释。见下面的例子。

[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 Address : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public String[] ShowAddress(string sLookUP)
    {
        List<string> lstAddresses = new List<string>();

        // YOUR CODE GO HERE

        return lstAddresses.ToArray();
    }
}