Skip to content

Instantly share code, notes, and snippets.

@ricardoalcocer
Created October 17, 2020 21:26
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 ricardoalcocer/7a4d9a07d08fbadfc763823aedc630c3 to your computer and use it in GitHub Desktop.
Save ricardoalcocer/7a4d9a07d08fbadfc763823aedc630c3 to your computer and use it in GitHub Desktop.
JavaScript XHR
function xhr(method, uri, body, handler) {
var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
req.onreadystatechange = function ()
{
if (req.readyState == 4 && handler)
{
eval('var o=' + req.responseText);
handler(o);
}
}
req.open(method, uri, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(body);
}
xhr('POST', 'url', 'querystring', function(responseText) {
// do something with the response
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment