Skip to content

Instantly share code, notes, and snippets.

@kirubz
Forked from jkk/xhr.js
Created January 30, 2017 12:55
Show Gist options
  • Select an option

  • Save kirubz/10a05b72e42dead47371103c649544e1 to your computer and use it in GitHub Desktop.

Select an option

Save kirubz/10a05b72e42dead47371103c649544e1 to your computer and use it in GitHub Desktop.
function postJson(method, url, params) {
let xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(JSON.stringify(params));
xhr.onreadystatechange = function() {
let status;
let data;
if (xhr.readyState === 4) {
status = xhr.status;
if (status === 200) {
data = JSON.parse(xhr.responseText);
console.log("success", data);
} else {
console.log("failed", status);
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment