title: $request->isXmlHttpRequest() returns false
body: <p>I am trying to send json data to my Symfony2 controller, using $.ajax method of jquery (1.12.4).</p>
  
  <p>My javascript:</p>
  
  <pre><code>         var category_type = 2;
           var json_data = JSON.stringify(category_type);
  
           $.ajax({
              type: "POST",
              url: Routing.generate('homepage'),
              contentType: 'application/json',
              dataType: "json",
              data: json_data,
              beforeSend: function (xhr){
                  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
              },
              success: function (result, status, xhr) {
                 console.log(result);
                 console.log(status);
              },
              error: function(xhr, status, error) {
                 console.log(error);
                 console.log(status);
              }
          });
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code> /**
   * @Route("/", name="homepage", options={"expose"=true})
   */
  public function indexAction(Request $request) {
  
      if($request-&gt;isXmlHttpRequest()){
          $content = $this-&gt;get("request")-&gt;getContent();
          $cat = json_decode($content, true);
          var_dump($content);
          exit;
      }
      else {
          echo 'Sorry!';
          exit;
      }
  </code></pre>
  
  <p>However I get the message Sorry! everytime. Now I am not sure if I used <code>xhr: setRequestHeader("X-Requested-With","XMLHttpRequest"),</code> correctly. But without it I get the same result.</p>
  
  <p>I've also tried removing the <code>if/else</code> condition and tried <code>$content = $this-&gt;get("request")-&gt;getContent();</code>. But when I do <code>var_dump($content);</code>, I get an empty string ie. <code>string '' (length=0)</code></p>
  
  <p><strong>Question:</strong></p>
  
  <ol>
  <li>Why does <code>$request-&gt;isXmlHttpRequest()</code> return <code>false</code>?</li>
  <li>How do I set the request header?</li>
  <li>Is this the correct way to send json data to the server(<code>$.ajax</code>)?</li>
  <li>Is this the correct way to receive data in the controller(<code>$this-&gt;get("request")-&gt;getContent();</code>)?</li>
  </ol>