【问题标题】:405 Method Not Allowed Error in WCFWCF 中的 405 方法不允许错误
【发布时间】:2011-01-27 00:42:16
【问题描述】:

有人能发现这个实现的问题吗?我可以在浏览器中打开它,它可以工作,但是来自客户端的调用(同时使用 jquery 和 asp.net ajax 失败)

服务合同

[OperationContract(Name = "GetTestString")]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json
   )]
string GetTestString();

在 Web.config 以及其他绑定中,我有一个 webHttp 绑定

<endpoint address="ajax" binding="webHttpBinding" contract="TestService" behaviorConfiguration="AjaxBehavior" />

端点行为

  <endpointBehaviors>
    <behavior name="AjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>

服务文件

<%@ ServiceHost Service="TestService" %>

客户

var serviceUrl = "http://127.0.0.1/Test.svc/ajax/";
var proxy = new ServiceProxy(serviceUrl);

然后我使用http://www.west-wind.com/weblog/posts/324917.aspx 中的方法 调用服务

【问题讨论】:

    标签: asp.net wcf


    【解决方案1】:

    链接上的示例使用 Http POST,而不是 Http GET。这就是“不允许的方法”——您需要更改代码以执行 GET。

    您发布的作为客户端代码来源的链接有这个块:

     $.ajax( { 
                    url: url,
                    data: json,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    timeout: 10000,
                    dataType: "text",  // not "json" we'll parse
    

    请注意其中的type: "POST" - 你的需要是“GET”。我假设您从发布的链接中获取了 JQuery,因为 405 状态表明您的 调用 代码是错误的,而不是服务。

    【讨论】:

    • 不确定你的意思。 GetTestString 具有带有 GET 选项的 WebInvoke 属性
    • 编辑了我的答案以获得更多细节(因为代码块不会很好地放在评论中)。
    • 谢谢!当我在代理 JS 中从 POST 更改为 GET 时,它开始工作。你知道为什么作者在从服务获取信息时选择使用 POST(我认为应该是 POST)
    • 任何 Web 服务都可以选择实现任何 Http 方法 - 其中最常见的是 GETPOSTPUTDELETEPOSTPUT 通常用于编写信息,因此在这方面,您链接的示例是不寻常的 - 一个名为 GetStockQuote 的方法似乎是一个奇怪的选择来实现为 POST - 但它是服务作者的选择 :-) 值得注意的是,您可以使用任何方法来返回结果(如果您愿意,可以使用 Http DELETE 返回信息!) - 它只是没有'不一定有道理!
    • 我希望我能为此投一百万票。错过这个我觉得很愚蠢。
    【解决方案2】:

    对于 method not allowed 错误,您需要检查的是确保您的 http web call /request 与服务中 [WebInvoke...] 中指定的相同

      $.ajax({
                    type: "POST",.....});
    

    应该与服务接口中指定的内容相同(在“[运营合同]”下)

     [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2017-11-13
      • 1970-01-01
      • 2021-12-20
      • 2016-12-02
      • 2015-11-25
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多