Created
October 17, 2020 21:26
-
-
Save ricardoalcocer/7a4d9a07d08fbadfc763823aedc630c3 to your computer and use it in GitHub Desktop.
JavaScript XHR
This file contains hidden or 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
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