【问题标题】:jQuery Ajax call to WCF service returning "Method not allowed (405)"jQuery Ajax 调用 WCF 服务返回“不允许的方法 (405)”
【发布时间】:2011-03-04 16:22:20
【问题描述】:

我使用 VS 2008 创建了一个在服务器端工作的 WCF SOAP 服务。我添加了下面的代码,使用 jQuery/json(没有 ASP.NET 脚本管理器)调用相同的服务/合同客户端。当我将服务 url 放入浏览器时,我得到了正确的服务页面,当我从 javascript 调用服务并通过 Firebug 跟踪时,我得到“405 方法不允许”。

ajax() 错误函数的 statusText 和 responseText 不包含任何内容,并且由于某种奇怪的原因,错误函数被调用了两次。我也在从 https 页面调用服务。

相同的 ajax 代码适用于服务器端和客户端类似的传统 Web 服务,也适用于页面方法。

有什么线索吗?我还注意到我的 Windows Communication Foundation HTTP 激活和非 HTTP 激活没有安装。我需要这些吗?我的服务器端 WCF 服务虽然可以工作。

界面:

[ServiceContract]
public interface ICust
{
    [OperationContract]
    [WebInvoke(Method = "POST", 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    bool GetCust(string zzz);
}

类:

public class Cust: ICust
{
    public bool GetCust(string zzz)
    {
        // Do Stuff
        // Return true/false; 
    }
 }

web.config:

<behaviors>
    <serviceBehaviors>
        <behavior name="E.W.CustomerBehavior" >
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="webCallable">
            <webHttp />
        </behavior>            
    </endpointBehaviors>
</behaviors>

<services>
    <service behaviorConfiguration="E.W.CustomerBehavior"
        name="E.W.Customer">
        <endpoint address="http://zzzz/Cust.svc" 
            binding="webHttpBinding" 
            contract="E.W.ICust" 
            behaviorConfiguration="webCallable">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
        <endpoint address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange" />
    </service>
</services>

jQuery:

$.ajax({
    cache: false,
    async: false,
    type: "POST",
    url: "http://zzzz/Cust.svc/GetCust",
    contentType: "application/json",
    dataType: "json",
    data: "{'zzz': '" + $("#ctl00_zz_zzz").val() + "'}",
    success: function(msg) {
        // do stuff
    },
    error: function(z) { alert(z.responseText); }
});

【问题讨论】:

    标签: c# jquery ajax wcf json


    【解决方案1】:

    问题是 https。调用服务的页面是 https,因此是“不允许的方法”。

    我更改了 web.config 和 javascript 文件以使用 https 调用服务并且它工作正常。

    This 帮了我一把。

    现在我明白为什么人们说 WCF 需要更多的前期工作。

    我安装了 Windows Communication Foundation HTTP Activation 和 Non-HTTP Activation,这与我的问题无关,但我仍然不清楚它们是什么。

    【讨论】:

      【解决方案2】:

      尝试在类Cust上添加以下属性

      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      

      【讨论】:

      • 我在 web.config 中有这个,但它不起作用。我确实将上述内容放在代码隐藏中,但没有运气。
      猜你喜欢
      • 2012-04-21
      • 2014-03-30
      • 2016-09-20
      • 2014-07-17
      • 2010-09-07
      • 2019-10-27
      • 2016-06-13
      • 2011-01-13
      相关资源
      最近更新 更多