title: Why does this AJAX POST fails with elasticsearch?
body: <p><strong>Summary</strong>: I want to get search results from an Elasticsearch instance via Javascript. The <code>POST</code> call fails, while the <code>GET</code> one is fine.</p>
  
  <p><strong>EDIT</strong>: I found the solution through <a href="https://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post">another question</a> (I will flag mine as a duplicate), though I do not understand why the need for <code>JSON.stringify()</code>.</p>
  
  <hr>
  
  <p><strong>Details</strong>: I am using the following function to request the total number of (filtered) hits:</p>
  
  <pre><code>function nrVulns() {
      $.ajax({
          type: "POST",
          crossDomain: true,
          url: 'http://elk.example.com:9200/scan/vulnerability/_search',
          data: {
              "query": {
                  "bool": {
                      "must": [
                          {
                              "match": {
                                  "risk_factor": "critical"
                              }
              }
           ]
                  }
              },
              "size": 0
          }
      }).success(function (data) {
          $('#nrvuln').text(data["hits"]["total"]);
  
      })
  }
  </code></pre>
  
  <p>It fails with a <code>400 Bad Request</code> reply from Elasticsearch:</p>
  
  <p><a href="https://i.stack.imgur.com/UBlty.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UBlty.png" alt="enter image description here"></a></p>
  
  <p>However:</p>
  
  <ul>
  <li>the same function with <code>type: "GET"</code> goes though, returning the total number of hits (not filtered by <code>query</code>). It looks like the <code>data</code> section is not taken into account (which is OK for a <code>GET</code> request)</li>
  <li>the <code>data</code> in the query above, copied and pasted verbatim into <a href="https://www.elastic.co/guide/en/sense/current/installing.html" rel="nofollow noreferrer">Sense</a> and sent though a <code>POST</code> yields correct results (<code>["hits"]["total"]</code> shows the correct number of hits, filtered by <code>query</code>)</li>
  <li>a <a href="http://jsonlint.com/" rel="nofollow noreferrer">JSONLint</a> confirms that the <code>data</code>  string is correct JSON.</li>
  </ul>
  
  <p><strong>What is wrong with the <code>POST</code> version of this query?</strong></p>