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

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

如何解决这个问题?


当前回答

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

改变

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

to

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

其他回答

极好的。

情况2 -同样的问题可以出现)在我的情况下,问题是由于以下一行:

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

它在服务器上工作得很好,因为直接调用webservice函数——但是如果你在调试环境中直接从。net运行服务,并且想要手动测试运行该函数,则会失败。

在这个网站上找到了一个解决方案

你所需要做的就是在你的web.config中添加以下内容

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

来自微软的更多信息

我也得到这个错误与apache mod-mono。看起来webservice的文档页在linux中还没有实现。但是尽管出现了这个错误,web服务仍然正常工作。您应该通过在url的末尾添加?WSDL来看到它,即http://localhost/WebService1.asmx?WSDL

确保你使用了正确的方法: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); }
})

我使用下面的代码行来解决这个问题。在web中编写以下代码。配置文件

<configuration>
    <system.web.extensions>
       <scripting>
       <webServices>
       <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
     </scripting>
   </system.web.extensions>
</configuration>