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

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

如何解决这个问题?


当前回答

一个需要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); }
})

我得到这个错误,直到我添加(如下所示的代码)$. 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>

一个需要ContextKey的WebMethod,

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

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

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

ac.ContextKey = "myKey";

在html中,你必须将调用包含在一个带有GET的表单中

<a href="/service/servicename.asmx/FunctionName/parameter=SomeValue">label</a>

您还可以使用POST,其操作是web服务的位置,并通过输入标记输入参数。

还有SOAP和代理类。

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

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