Skip to content

Instantly share code, notes, and snippets.

@koistya
Created February 1, 2014 21:07
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 koistya/8758886 to your computer and use it in GitHub Desktop.
Save koistya/8758886 to your computer and use it in GitHub Desktop.
Send Ajax request with vanilla JavaScript
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/sum?a=1&b=2', false);
xhr.send(null);
console.log(xhr.responseText); // prints 3
@ben-turner
Copy link

You should use this for better cross-browser support (IE 5, IE 6).

var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment