Skip to content

Instantly share code, notes, and snippets.

@hyeonseok
Last active December 12, 2015 05:45
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 hyeonseok/604812e389aa9e74d346 to your computer and use it in GitHub Desktop.
Save hyeonseok/604812e389aa9e74d346 to your computer and use it in GitHub Desktop.
Minimal Ajax function
// Minimal Ajax function https://gist.github.com/hyeonseok/604812e389aa9e74d346
function ajax(u,c,d){var x=new(this.XMLHttpRequest||ActiveXObject)("Microsoft.XMLHTTP");x.onreadystatechange=function(){this.readyState^4||c(this)};if(d){x.open('POST',u);x.setRequestHeader('Content-type','application/x-www-form-urlencoded')}else{x.open('GET',u)}x.send(d)}
/*
GET request
ajax('whatever.php?foo=bar', function (xhr) {
console.log(xhr.responseText);
});
POST request
ajax('whatever.php', function (xhr) {
console.log(xhr.responseText);
}, 'foo=bar');
Inspired by
- https://code.google.com/p/microajax/
- https://gist.github.com/azproduction/1625623
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment