当消费WebService时,我得到了以下错误:

URL意外以/myMethodName结尾,请求格式无法识别

如何解决这个问题?


当前回答

以我为例,当我从本地PC的Windows 10移动到安装Windows 2012的专用服务器时,出现了这个错误。 解决方案是添加到网络。配置以下行

<webServices>
        <protocols>
               <add name="Documentation"/>
        </protocols>
</webServices>

其他回答

我得到这个错误,直到我添加(如下所示的代码)$. holdready (true)在我的web服务调用的开始和$. holdready (false)结束后。这是jQuery的东西挂起页面的就绪状态,所以文档中的任何脚本。就绪函数将等待这个(在其他可能但我不知道的事情中)。

<span class="AjaxPlaceHolder"></span>
<script type="text/javascript">
$.holdReady(true);
function GetHTML(source, section){
    var divToBeWorkedOn = ".AjaxPlaceHolder";
    var webMethod = "../MyService.asmx/MyMethod";
    var parameters = "{'source':'" + source + "','section':'" + section + "'}";

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        xhrFields: {
            withCredentials: false
        },
        crossDomain: true,
        success: function(data) {
            $.holdReady(false);
            var myData = data.d;
            if (myData != null) {
                $(divToBeWorkedOn).prepend(myData.html);
            }
        },
        error: function(e){
            $.holdReady(false);
            $(divToBeWorkedOn).html("Unavailable");
        }
    });
}
GetHTML("external", "Staff Directory");
</script>

以我为例,当我从本地PC的Windows 10移动到安装Windows 2012的专用服务器时,出现了这个错误。 解决方案是添加到网络。配置以下行

<webServices>
        <protocols>
               <add name="Documentation"/>
        </protocols>
</webServices>

为了记录,当我将一个旧应用程序从一个服务器移动到另一个服务器时,我得到了这个错误。我添加了<add name="HttpGet"/> <add name="HttpPost"/>元素到web。Config,它将错误更改为:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at BitMeter2.DataBuffer.incrementCurrent(Int64 val)
   at BitMeter2.DataBuffer.WindOn(Int64 count, Int64 amount)
   at BitMeter2.DataHistory.windOnBuffer(DataBuffer buffer, Int64 totalAmount, Int32 increments)
   at BitMeter2.DataHistory.NewData(Int64 downloadValue, Int64 uploadValue)
   at BitMeter2.frmMain.tickProcessing(Boolean fromTimerEvent)

为了修复这个错误,我必须添加ScriptHandlerFactory行到web.config:

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

为什么在一个服务器上没有这些行,而在另一个服务器上没有,我不知道。

确保你使用了正确的方法:Post/Get,正确的内容类型和正确的参数(数据)。

$.ajax({
    type: "POST",
    url: "/ajax.asmx/GetNews",
    data: "{Lang:'tr'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) { generateNews(msg); }
})

确保您禁用了自定义错误。这可以掩盖代码中的原始问题:

改变

<customErrors defaultRedirect="~/Error" mode="On">

to

<customErrors defaultRedirect="~/Error" mode="Off">