Skip to content

Instantly share code, notes, and snippets.

@rampatra
Created April 13, 2016 10:19
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 rampatra/969924215b724c19faf795497f7274ad to your computer and use it in GitHub Desktop.
Save rampatra/969924215b724c19faf795497f7274ad to your computer and use it in GitHub Desktop.
Generic XML Http Request method in javascript
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);
}
@rampatra
Copy link
Author

You call the above method like this:

xhr('POST', 'chat', 'q1=somedata&q2=otherdata', 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