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

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

如何解决这个问题?


当前回答

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

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

其他回答

一个需要ContextKey的WebMethod,

[WebMethod]
public string[] GetValues(string prefixText, int count, string contextKey)

当此键未设置时,得到异常。

通过分配AutoCompleteExtender的键来修复它。

ac.ContextKey = "myKey";

确保你使用了正确的方法: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服务器不支持函数重载

我在本地主机开发时没有这个问题。然而,一旦我发布到一个web服务器,web服务返回一个空(空白)的结果,我看到我的日志中的错误。

我通过设置我的ajax contentType来修复它:

"application/json; charset=utf-8"

并使用:

JSON.stringify()

在我张贴的物体上。

var postData = {data: myData};
$.ajax({
                type: "POST",
                url: "../MyService.asmx/MyMethod",
                data: JSON.stringify(postData), 
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                },
                dataType: "json"
            });

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

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