Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active April 18, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpscholar/7b71c0850171e966c482fec3b80fd136 to your computer and use it in GitHub Desktop.
Save wpscholar/7b71c0850171e966c482fec3b80fd136 to your computer and use it in GitHub Desktop.
var url = "";
var params = {
name: 'John',
email: 'webmaster@mysite.com'
};
var xhr = new XMLHttpRequest();
xhr.open('POST', url + '?' + buildQueryString(params));
xhr.responseType = 'json';
xhr.send(null);
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
console.log(xhr.response); // 'This is the returned text.'
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
}
}
function buildQueryString( params ) {
return Object.keys(params).map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
}).join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment