Last active
April 18, 2019 20:49
-
-
Save wpscholar/7b71c0850171e966c482fec3b80fd136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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