0

I am building simple WCF web service for my mobile web app.

I am stack at the point of making the $.ajax request to WCF which comes back with 405 - Method not allow, even though when I do http://http://localhost:35798/RestServiceImpl.svc/json/23 I can see result of the ReturnJSONData() in the browser.

I went through 100's of different post, but none of the answer fixed my problem.

AJAX Request

$.ajax({
    type: "GET",
    url: "http://localhost:35798/RestServiceImpl.svc/json/34",
    contentType: "application/json; charset=utf-8", 
    success: function(data) {
    console.log(data);
    },
});

IRestServiceImpl.cs

namespace RestService{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}"
            )]
        string ReturnJSONData(string id);
    }
}

RestServiceImpl.svs.cs

namespace RestService {
    public class RestServiceImpl : IRestServiceImpl {
        public string ReturnJSONData(string id) {
            return "You requested product " + id;
        }
    }
}

WebConfig

<?xml version="1.0"?>
<configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
    <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
    </service>
</services>

<behaviors>
    <serviceBehaviors>
        <behavior name="ServiceBehaviour">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<standardEndpoints>
    <webHttpEndpoint>
        <standardEndpoint name=""
        helpEnabled="true"
        automaticFormatSelectionEnabled="true"
        defaultOutgoingResponseFormat ="Json"
        crossDomainScriptAccessEnabled="true"/>
    </webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

Any suggestion much appreciated.

Iladarsda
  • 10,640
  • 39
  • 106
  • 170
  • Are you sure that the URL isn't supposed to be a relative path...? url: "/RestServiceImpl.svc/json/34", – Koenyn Nov 13 '12 at 12:20
  • You probably find that it's a browser security setting that prevents you from doing that in javascript. though accessing WCF services in JS isn't something I've done before – Koenyn Nov 13 '12 at 12:21
  • Did you check those links: http://stackoverflow.com/questions/41155/wcf-service-returning-method-not-allowed http://stackoverflow.com/questions/3057124/jquery-ajax-call-to-wcf-service-returning-method-not-allowed-405?rq=1 – Thiago Custodio Nov 13 '12 at 12:22
  • @ThiagoCustodio - yes, I also had a go with WebGet - no success.. – Iladarsda Nov 13 '12 at 12:41
  • @Koenyn - web app will be running on a different domain than web service. So path for sure will not be relative. – Iladarsda Nov 13 '12 at 12:41

0 Answers0