Search code examples
jqueryweb-serviceswcfwcfserviceclientsmartystreets

WCF called from Jquery: Method not allowed


I've read countless walkthroughs of creating WCF services today, and after trying too many times, I've arrived at this point and have to give up for the day to keep my sanity. I am attempting to test an API (SmartyStreets), retrieving the test address they list on their site and getting back responses in JSON format.

I have a ISmartySreets.cs:

[ServiceContract]
public interface ISmartyStreetsService
{
    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string AddressLookup(string learnersID, string street, string secondary, string city, string state, string zip);
}

I have a SmartyStreets.svc.cs file:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SmartyStreetsService : ISmartyStreetsService
{
    public string AddressLookup(string street, string secondary, string city, string state, string zip)
    {
        //Lookup returns JSON of addresses, or string error.
        return Lookup(street, secondary, city, state, zip);
    }
}

I have a Frankenstein web.config that is the victim of constant edits from my research:

  <?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
    <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SmartyStreetsWCF.SmartyStreetsService" behaviorConfiguration="serviceBehavior">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="SmartyStreetsWCF.ISmartyStreetsService"
                  behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
        <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

And lastly, I have a very modest HTML file using Jquery to call my service, hosted on a non-local web server.

$.ajax({
        url: "http://wcftesting2.com/SmartyStreets.svc/AddressLookup",
        type: "GET",
        data: {street: "1 Rosedale St", secondary: "", city: "Baltimore", state:"MD", zip:"21229"},
        contentType: "application/json",
        dataType: "json",
        success: function(addresses) {
            alert("EUREKA!");
        },
        error: function(error) {
            alert("FAILURE");
        }
    });

Attempting to run the jquery from my HTML page I get the following: Response for preflight has invalid HTTP status code 405

The "OPTIONS" are as such: /SmartyStreets.svc/AddressLookup?street=1+Rosedale+St&secondary=&city=Baltimore&state=MD&zip=212

Navigating to the link: http://wcftesting2.com/SmartyStreets.svc/AddressLookup?learnersID=12345&street=1+Rosedale+St&secondary=&city=Baltimore&state=MD&zip=21229 just says, "method not allowed".

I assume my problem is somewhere between the web.config file and how I am calling the service with Jquery, but I need to stop working on this for an evening.

If anyone can help me out in the slightest way, that would be amazing.


Solution

  • You have to remove contentType because you are passing data in GET not POST.

    I refer following link.

    Differences between contentType and dataType in jQuery ajax function on stack overflow

    jquery documentation for ajax