title: How to add cross domain support to WCF service
body: <p>I'm trying to allow POST requests from my javascript app hosted at localhost:80 to a WCF REStful service hosted at a different port, but somehow it doesn't work. I've tried adding custom properties to the header, as well as adding it programatically in my service's <code>JSONData</code> method but I'm still getting '405 Method not allowed' in my response. What is the proper approach here ? </p>
  
  <p>This is my interface :</p>
  
  <pre><code>namespace RestService
  {
      public class RestServiceImpl : IRestServiceImpl
      {
          #region IRestServiceImpl Members
  
          public string JSONData()
          {
              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
              return "Your POST request";
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>and the service code :</p>
  
  <pre><code>using System.ServiceModel;
  using System.ServiceModel.Web;
  using System.Web.Script.Services;
  
  namespace RestService
  {
  
      [ServiceContract]
      public interface IRestServiceImpl
      {
          [OperationContract]
          [ScriptMethod]
          [WebInvoke(Method = "POST",
              ResponseFormat = WebMessageFormat.Json,
              BodyStyle = WebMessageBodyStyle.Bare,
              UriTemplate = "export")]
          string JSONData();
      }
  }
  </code></pre>
  
  <p>And finally the config :</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"&gt;
          &lt;endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"&gt;
          &lt;/endpoint&gt;
        &lt;/service&gt;
      &lt;/services&gt;
  
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="ServiceBehaviour"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="web"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
      &lt;httpProtocol&gt;
        &lt;customHeaders&gt;
           &lt;add name="Access-Control-Allow-Origin" value="*" /&gt;
        &lt;/customHeaders&gt;
  &lt;/httpProtocol&gt;  
    &lt;/system.webServer&gt;
  
  &lt;/configuration&gt;
  </code></pre>