title: What are HTTP request headers?
body: <p><a href="https://stackoverflow.com/questions/2999792/posting-a-form-using-ajax/2999946#2999946">Posting a form using AJAX</a></p>
  
  <p>My AJAX script wasn't working before (it wouldn't send data) but setting the request headers solve the problem. It's great that it works but I want to understand why they are needed for it to work. Thanks :-)</p>
  
  <pre><code>//Send the proper header information along with the request 
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
  http.setRequestHeader("Content-length", params.length); 
  http.setRequestHeader("Connection", "close"); 
  </code></pre>
  
  <p>javascript </p>
  
  <pre><code>function request(elm, type, url, str, fn) { 
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange=function()
  {
      if (xhr.readyState==4 &amp;&amp; xhr.status==200)
      {
          if (!fn) elm.innerHTML=xhr.responseText; 
          else fn(xhr);
      }
  }
        xhr.open(type, url, true);
        //yay it works with this :-)
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send(str);
  }
  
  var form = document.getElementById("form_login")
  var btnLogin = form.getElementsByClassName("btn")[0];
  addEvent(btnLogin, "click", function(e) 
  {
      preventDefault(e);
      var post ="",input,inputs = form.getElementsByClassName("input");
      for (var i=0, l=inputs.length; i&lt;l; i++) 
      {
          input = inputs[i].getElementsByTagName("input")[0];
          post += input.name + "=" + encodeURI(input.value) + "&amp;";
      }
      post = post.substr(0,post.length-1);
      var help = form.getElementsByClassName("help")[0];
      request(help, "POST", "user/login-exe.php?dt='" + new Date() + "'", post);
  });
  </code></pre>
  
  <p>and if its all valid it will log the user in that bit isn't shown though.</p>