title: WCF returns Bad Request on jquery ajax call
body: <p>I have a WCF Service with one GET and one POST method. The GET works well.</p>
  
  <p>The frontend is a simple HTML page which uses jquery ajax calls to GET and POST methods of the WCF.</p>
  
  <p>The problem is that i get a Bad Request response. I have read all of the relative posts and i think that everything is in order, as far as i can tell.</p>
  
  <p>Sources:</p>
  
  <p>My contracts:</p>
  
  <pre><code>[OperationContract]
          [WebGet(UriTemplate = "schedule?week={week}",
                  ResponseFormat = WebMessageFormat.Json,
                  BodyStyle = WebMessageBodyStyle.Bare)]
          [Description("Return the food schedule; specify the dates to include.")]
          WeeklySchedule GetSchedule(int week);
  
          [OperationContract]
          [WebInvoke(UriTemplate="writeweek", Method = "POST", 
                      RequestFormat=WebMessageFormat.Json,
                      ResponseFormat=WebMessageFormat.Json,
                      BodyStyle=WebMessageBodyStyle.Bare)]
          [Description("Write or Update a week schedule via json data with POST")]
          int WriteWeek(string jsonwk);
  </code></pre>
  
  <p>I have a separate Data Access Layer which does the write/read in DB and works fine.</p>
  
  <p>My page source regarding the ajax calls:</p>
  
  <pre><code>    function ajaxGetWeek(wk)
      {
          $.ajax({
              cache: false,
              type: "GET",
              processData: false,
              url: 'http://appsrv01/food/svc/foodland.svc/schedule?week='+ wk,
              dataType: "json",
              success: function(data) {
                      GetWeekSucceeded(data);
              },
              error: function (xhr,status,error) {
                  alert(error);
              }
          });
  </code></pre>
  
  <p>This works fine, it returns json format and i am able to parse it in my form.</p>
  
  <p>POST ajax call that returns bad request:</p>
  
  <pre><code>function SubmitFood() {
          var features = new Object();    // Create empty javascript object
          features["weeknumber"] = currentWeek;
          $("#TheForm textarea").each(function() {           // Iterate over inputs
              features[$(this).attr("name")] = $(this).val();  // Add each to features object
          });         
          var jsontxt = JSON.stringify(features);
          $.ajax({
              type: "POST",
              url: 'http://appsrv01/food/svc/foodland.svc/writeweek',
              data: jsontxt,
              dataType: "json",
              contentType: "application/json; charset=utf-8",
              success: function(postresult) {
                      WriteWeekSucceeded(postresult);
              },
              error: function (xhr,status,error) {
                  alert(error);
              }
          });
  
      }
  </code></pre>
  
  <p>The above returns something like:</p>
  
  <pre><code>{"weeknumber":24,"DishMon":"sdfadsf","DishTue":"asdfasd","DishWed":"fasdfa","DishThu":"sdfasdfas","DishFri":"dfasdf"}
  </code></pre>
  
  <p>i have put breakpoints in my VS solution but it dsoesnt even get to call the method.</p>
  
  <p>my app.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
    &lt;system.web&gt;
      &lt;compilation debug="true"/&gt;
    &lt;/system.web&gt;
    &lt;!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="FoodLandService.FoodLandService"&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost:6969/FoodLand"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
          &lt;endpoint address="" 
                    binding="webHttpBinding" 
                    contract="ContractsClass.IFoodLandService" 
                    behaviorConfiguration="Web"  /&gt;
          &lt;endpoint address="mex" 
                    binding="mexHttpBinding" 
                    contract="IMetadataExchange"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="Web"&gt;
            &lt;webHttp helpEnabled="true"/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="True" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="False"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  
  &lt;startup&gt;&lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt;&lt;/startup&gt;&lt;/configuration&gt;
  </code></pre>
  
  <p>web.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
    &lt;system.web&gt;
      &lt;compilation debug="false" targetFramework="4.0" /&gt;
      &lt;customErrors mode="Off"/&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>and foodland.svc:</p>
  
  <pre><code>&lt;%@ ServiceHost Service="FoodLandService.FoodLandService"
       Factory="System.ServiceModel.Activation.WebServiceHostFactory" %&gt;
  </code></pre>
  
  <p>My Service source:</p>
  
  <pre><code>public class FoodLandService : ContractsClass.IFoodLandService
  {
      public WeeklySchedule GetSchedule(int week)
      {
          FoodClassDB fd = new FoodClassDB();
          return fd.ReadWeekDB(week);
      }
  
      public int WriteWeek(string jsonwk)
      {
          FoodClassDB fd = new FoodClassDB();
          return fd.WriteWeekDB(jsonwk);
      }
  }
  </code></pre>
  
  <p>json is encoded fine and i cant find a reason for my error.</p>