Skip to content

Instantly share code, notes, and snippets.

@jkk
Created July 27, 2015 11:24
Show Gist options
  • Save jkk/d057133bec382b19c913 to your computer and use it in GitHub Desktop.
Save jkk/d057133bec382b19c913 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