WCF restful service:服务器响应状态为405时出错(Method Not Allowed)

时间:2014-03-24 11:23:22

标签: jquery asp.net ajax wcf rest

我知道它看起来像重复的问题。

这里我想使用jquery ajax调用我的wcf rest服务,并希望在rest服务中传递countryname作为参数的country对象。但每当我使用来自html page的jquery ajax调用我的休息服务。但是它给了我错误405 method not allowed。我尝试了很多时间来解决它。但是我能够解决这个错误。我正在尝试在json对象中传递数据。

Iservice.cs:

[OperationContract]
    [WebInvoke(UriTemplate = "/AddCountry", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
    String AddCountry(tableCountry Country);

Service.cs

public string AddCountry(tableCountry Country)
{
    //do Code.
}

的Web.config

<system.serviceModel>
        <bindings>
            <webHttpBinding>
                <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true">
                    <security mode="None"/>
                </binding>
            </webHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="webbehaviour">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service name="Service">
                <endpoint address="rest" binding="webHttpBinding" contract="IService" bindingConfiguration="webHttpBinding" behaviorConfiguration="webbehaviour"/>
            </service>
        </services>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>  
</system.serviceModel>
<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true"/>
   <directoryBrowse enabled="true"/>
</system.webServer>

Ajax代码

$("document").ready(function(){    
    var country = {"CountryName":"Iran"};

    $.ajax({
        type: "POST",                   
        url: "http://localhost:2293/ACFRestAjaxParsing/Service.svc/rest/AddCountry",    
        data: JSON.stringify({ Country: country }),                 
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
});

我引用此链接来解决此错误Stackoverflow

如果有人在我的情况下知道这个错误,请帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

<强> Iservice.cs

[OperationContract]
    [WebInvoke(UriTemplate = "/AddCountry", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
    String AddCountry(tableCountry Country);

public class list_UserContact :  List<tableCountry>
{

}

<强> Service.cs

public string AddCountry(tableCountry Country)
{
    //do Code.
}

<强>的Web.config

<system.serviceModel>
        <bindings>
            <webHttpBinding>
                <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true">
                    <security mode="None"/>
                </binding>
            </webHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="webbehaviour">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service name="Service">
                <endpoint address="rest" binding="webHttpBinding" contract="IService" bindingConfiguration="webHttpBinding" behaviorConfiguration="webbehaviour"/>
            </service>
        </services>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>  
</system.serviceModel>
<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true"/>
   <directoryBrowse enabled="true"/>
</system.webServer>

Ajax代码

              

    function CallWCF() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: 'http://yourliveurl/ACFRestAjaxParsing/Service.svc/rest/AddCountry',
            data: '[{"CountryId":2147483647,"CountryName":"String content"}]',
            processData: true,
            dataType: "json",
            success: ServiceSucceeded,
            error: ServiceFailed
        });
    }

    function ServiceFailed(output) {
        Log('Service call failed: ' + output.status + ' ' + output.statusText);
    }

    function ServiceSucceeded(output) {
        var outputValue = output;
        Log("Service call Success: <br/> " + outputValue);
    }

    function Log(displayValueFromService) {
        $("#DisplayOutput").append('<br/>' + displayValueFromService);
    }
</script>

              

执行命令

url:http://localhost:2293/ACFRestAjaxParsing/Service.svc/rest/AddCountry
Header Content :Application/json
Body:Your json data

您的服务将以live ip

托管

Html文件将以live ip

托管