开发者

jQuery Ajax call to WCF service returning "Method not allowed (405)"

I created a WCF SOAP service using VS 2008 that works server side. I tacked on the code below to call the same service/contract client side using jQuery/json (no ASP.NET scriptmanager). When I put the service url into the browser, I get the correct service page and when I invoke the service from javascript and trace via Firebug, I get "405 method not allowed".

The ajax() error function's statusText and and responseText contain nothing and the error function gets called twice for some strange reason. I'm also invoking the service from an https page.

The same ajax code works with a similar traditional webservice both server side and client side and also works with a page method.

Any clues? I also noticed my Windows Communication Foundation HTTP Activation and Non-HTTP Activation are not installed. Do I need these? My server side WCF service works though.

Interface:

[ServiceContract]
public interface ICust
{
    [OperationContract]
    [WebInvoke(Method = "POST", 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    bool开发者_运维百科 GetCust(string zzz);
}

Class:

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); }
});


Try put following attribute on the class Cust

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]


The problem was https. The page from which the service was invoked was https, thus the "method not allowed".

I changed the web.config and javascript file to invoke the service using https and it worked.

This helped me out.

Now I am seeing why people are saying WCF is more work up front.

I installed Windows Communication Foundation HTTP Activation and Non-HTTP Activation, which has nothing to do with my issue, but I'm still not clear on what they are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜