title: How to post data to WCF Service using jquery Ajax?
body: <p>I am facing issue in consuming WCF service using JQUERY AJAX. I know this is cross domain issue and have read alot of solution about it. but none worked for me. Below is all the related code. Can anyone please help me out?</p>
  
  <p>Thanks</p>
  
  <pre><code> [OperationContract]
          [WebInvoke(Method = "POST",BodyStyle=WebMessageBodyStyle.Bare,
              RequestFormat = WebMessageFormat.Json,
             ResponseFormat = WebMessageFormat.Json)]
          [return: MessageParameter(Name = "result")]
  
  
          public ServiceSearchResponse GetSearchResults(ServiceSearchParams sParams)
          {
              /// search function
          }
  </code></pre>
  
  <p>JQUERY:</p>
  
  <pre><code>        $.ajax({
              type: 'POST',
              url: "http://myserviceurl.com/GetSearchResults",
              data: p,
              contentType: "application/json; charset=utf-8",
              dataType: 'json',
              crossDomain: true,
              success: function (data) {
  
                  alert(data);
              },
              failure: function (response) {
                  alert('failed');
              },
              error: function (response) {
                  alert(JSON.stringify(response));
              }
          });
  </code></pre>
  
  <p>Webconfig:</p>
  
  <pre><code>  &lt;system.webServer&gt;        
          &lt;httpProtocol&gt;
            &lt;customHeaders&gt;
              &lt;add name="Access-Control-Allow-Origin" value="*" /&gt;
              &lt;add name="Access-Control-Allow-Headers" value="Content-Type" /&gt;
            &lt;/customHeaders&gt;
          &lt;/httpProtocol&gt;
        &lt;/system.webServer&gt;
  
  &lt;system.serviceModel&gt;
      &lt;protocolMapping&gt;
        &lt;add scheme="http" binding="webHttpBinding" bindingConfiguration="" /&gt;
      &lt;/protocolMapping&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="DefaultServiceBehavior"&gt;
            &lt;!--Added DefaultServiceBehavior referenced at service tag above--&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;       
          &lt;behavior name="myserives.services.AppServicesAspNetAjaxBehavior"&gt;
  
            &lt;webHttp /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
      &lt;services&gt;  
        &lt;service name="mypackage.services.MyServices"&gt;
          &lt;endpoint address="" behaviorConfiguration="myserives.services.AppServicesAspNetAjaxBehavior" binding="webHttpBinding"
             bindingConfiguration="LargeSizeMessages" contract="myContractName"  /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="LargeSizeMessages" maxBufferSize="2147483647"
                   maxBufferPoolSize="2147483647"
                   maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true"&gt;      
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
        &lt;/webHttpBinding&gt;
  
      &lt;/bindings&gt;
  
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>This is how it looks like:
  <a href="http://screencast.com/t/b7tsqld6" rel="nofollow">http://screencast.com/t/b7tsqld6</a></p>
  
  <p>See error :
  <a href="http://screencast.com/t/pWQNAlmMYS3" rel="nofollow">http://screencast.com/t/pWQNAlmMYS3</a></p>
  
  <p>And nothing in post data. though i m posting data.</p>
  
  <p>--- UPDATE</p>
  
  <p>See my Global.asax .. i am doing something wrong here:</p>
  
  <pre><code> protected void Application_BeginRequest(object sender, EventArgs e)
          {
  
              HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
              HttpContext.Current.Response.Cache.SetNoStore();
  
              EnableCrossDmainAjaxCall();
          }
  
          private void EnableCrossDmainAjaxCall()
          {
              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
                            "*");
  
              if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
              {
                  HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                                "GET, POST");
                  HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials",
                               "true");
                  HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                                "Content-Type, Accept");
                  HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
                                "1728000");
                  HttpContext.Current.Response.End();
              }
          }
  </code></pre>