title: Cross Domain AJAX Post - WCF
body: <p>I have the following web service created and using the latest version of jquery I need to post to the web service.  </p>
  
  <p>I can not figure this out.  I've read that JSONP won't work with a POST.  How do I get this to work?</p>
  
  <p>I need to do a cross domain post using jQuery to WCF.  </p>
  
  <p>service.cs:</p>
  
  <pre><code>namespace AjaxPost
  {
      [DataContractAttribute]
      public class Message
      {
        [DataMemberAttribute]
        public string success;
  
        public Message(string success)
          this.success = success;
      }
  
  
      [ServiceContract(Namespace="JsonpAjaxService")]
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class User
      {
          [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="POST")]
          public Message CreateUser(string email, string username, string password, string phone, string image)
          {
             Message msg = new Message("true");
             return msg;
          }
      }
  }
  </code></pre>
  
  <p>service.svc:</p>
  
  <pre><code>&lt;%@ServiceHost 
    language="c#"
    Debug="true"
    Service="Microsoft.Samples.Jsonp.CustomerService"
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" 
  %&gt;
  </code></pre>
  
  <p>Service Web.Config:</p>
  
  <pre><code>&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;add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" /&gt;
        &lt;/customHeaders&gt;
    &lt;/httpProtocol&gt;
  &lt;/system.webServer&gt;
  
  &lt;system.serviceModel&gt;        
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="MetadataBehavior"&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="True" httpHelpPageEnabled="True" /&gt;
                  &lt;serviceMetadata httpGetEnabled="True"/&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"/&gt;
      &lt;standardEndpoints&gt;
          &lt;webScriptEndpoint&gt;
              &lt;standardEndpoint name="" crossDomainScriptAccessEnabled="true"/&gt;
          &lt;/webScriptEndpoint&gt;
      &lt;/standardEndpoints&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>    wcfServiceUrl = "http://localhost:33695/Service.svc/CreateUser";
  
      $.ajax({
          crossDomain: true,
          cache: true,
          url: wcfServiceUrl,
          data: "{}",
          type: "POST",
          jsonpCallback: "Message",
          contentType: "application/json",
          dataType: "json",
          data: "{ \"myusername\": \"mypassword\" }",
          error: function (request, status, error) {
              //error loading data
              alert("error");
          },
          success: function (menu) {
              alert('success');
          }
      });
  </code></pre>