Skip to content

Instantly share code, notes, and snippets.

@0xKD
Last active January 25, 2016 09:59
Show Gist options
  • Save 0xKD/19b47356ad4751728a48 to your computer and use it in GitHub Desktop.
Save 0xKD/19b47356ad4751728a48 to your computer and use it in GitHub Desktop.
Make HTTP POST using XMLHttpRequest
var xhr = new XMLHttpRequest();
url = '/url/to/post';
params = JSON.stringify({'key': 'value'});
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Content-Length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.onreadystatechange = function() {
if (this.status == 200) {
console.log(this.responseText);
}
}
xhr.send(params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment