Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

WCF restful service: Error the server responded with a status of 405 (Method Not Allowed)

i know it's looks like duplicate question.

Here i want to call my wcf rest service using jquery ajax and want to pass country object which have countryname as parameter in rest service. but whenever I am calling my rest service using jquery ajax from html page.but it is giving me error 405 method not allowed.I tried lots of time to solve it.but i am enable to solve this error.And i am trying to pass data in json object.

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 Code

$("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);
        }
    });
});

I refer this link to solve this error Stackoverflow

If anyone have idea about this error in my case then please help me.

Thanks in advance.

Upvotes: 0

Views: 6215

Answers (1)

pankeel
pankeel

Reputation: 1148

Try This:

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 Code

    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>

Run:

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

your service will be hosted in live ip

Html File will be hosted in live ip

Upvotes: 1

Related Questions