jQuery的Ajax调用WCF服务返回"不允许的方法(405)QUOT;不允许、方法、Ajax、jQuery

2023-09-10 14:04:38 作者:想要两清

我在使用VS 2008的作品服务器端WCF SOAP服务。我就上涨了code以下调用使用jQuery / JSON(不ASP.NET的ScriptManager)相同的服务/合同的客户端。当我把服务网址到浏览器中,我得到了正确的服务页面,当我从JavaScript通过Firebug的调用服务和跟踪,我得到不允许的405方法。

阿贾克斯()误差函数的状态文本和和的responseText包含什么,误差函数被调用两次一些奇怪的原因。我也从HTTPS页面调用服务。

同阿贾克斯code工作具有类似传统的web服务包括服务器端和客户端,也适用于一个页面的方法。

任何线索?我也注意到我的不安装Windows通信基础HTTP激活和非HTTP激活。我需要这些?我的服务器端的WCF服务的工作虽然。

接口:

  [的ServiceContract]
公共接口ICust
{
    [OperationContract的]
    [WebInvoke(方法=POST
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)
    布尔GetCust(字符串ZZZ);
}
 

类:

 公共类卡斯特:ICust
{
    公共BOOL GetCust(字符串ZZZ)
    {
        //做的东西
        //返回真/假;
    }
 }
 

web.config中:

 <行为>
    < serviceBehaviors>
        <行为NAME =E.W.CustomerBehavior>
            < serviceMetadata httpGetEnabled =真/>
        < /行为>
    < / serviceBehaviors>
    < endpointBehaviors>
        <行为NAME =webCallable>
            < webHttp />
        < /行为>
    < / endpointBehaviors>
< /行为>

<服务>
    <服务behaviorConfiguration =EWCustomerBehavior
        NAME =E.W.Customer>
        <端点地址=HTTP://zzzz/Cust.svc
            绑定=的WebHttpBinding
            合同=E.W.ICust
            behaviorConfiguration =webCallable>
            <身份>
                < D​​NS值=本地主机/>
            < /身分>
        < /端点>
        <端点地址=MEX
            绑定=mexHttpBinding
            合同=IMetadataExchange接口/>
    < /服务>
< /服务>
 
asp.net webservice如何获取jquery ajax post请求的参数

jQuery的:

  $。阿贾克斯({
    缓存:假的,
    异步:假的,
    键入:POST,
    网址:HTTP://zzzz/Cust.svc/GetCust
    的contentType:应用/ JSON
    数据类型:JSON,
    数据:{ZZZ:+ $(#ctl00_zz_zzz)VAL()+'},
    成功:函数(MSG){
        //做的东西
    },
    错误:函数(Z){警报(z.responseText); }
});
 

解决方案

问题是HTTPS。从该服务被调用是HTTPS的页面,因此方法不允许的。

我改变了的web.config和JavaScript文件使用https来调用服务和它的工作。

这帮了我。

现在我看到人们为什么说WCF是更多的工作在前面。

我安装了Windows Communication Foundation的HTTP激活和非HTTP激活,其中有无关,与我的问题,但我仍然不清楚它们是什么。

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

解决方案

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.