title: XMLHttpRequest POST to PHP
body: <p>I'm having some trouble managing the answers to a simple XMLHttpRequest made on JS to my own server. I have some troubling answers, here's my code:</p>
  
  <p>JavaScript:</p>
  
  <pre><code>function callPHP () {
      var xml = new XMLHttpRequest();
      xml.open("POST", "http://localhost/ajaxTest", false);
      xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xml.send("format=json");
      var resp = xml.responseText;
      console.log(resp);
  }
  </code></pre>
  
  <p>And PHP:</p>
  
  <pre><code>&lt;?php
      if (isset($_POST["format"])){
          if($_POST["format"] == "json"){
              echo '
                  {
                      "name": "Name",
                      "lastName": "LastName", 
                      "dob" : "dd/mm/yyyy",
                  }
                  ';
          }
      }else{
          print_r($_POST);
          echo "Error";
          http_response_code(400);
      }
  
  
  ?&gt;
  </code></pre>
  
  <p>And whenever I execute my JS I get the error part of the PHP code, even having sent the "format=json" data on request. But if I change asynchronous to true, I get a GET error (in chrome's console) of 400 Bad Request, but no echo of the PHP is executed.</p>
  
  <p>I know I have to check the xmlhttprequest status and response code to do asynchronous, I was testing it directly on the console. </p>
  
  <p>I don't know what I'm doing wrong here, the POST data should be sent, and independently of asynchronous or not, right?</p>
  
  <p>Thank you all!</p>