Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created September 28, 2016 07:39
Show Gist options
  • Select an option

  • Save rheajt/c8d37b0b60eab3eb841822094270cac3 to your computer and use it in GitHub Desktop.

Select an option

Save rheajt/c8d37b0b60eab3eb841822094270cac3 to your computer and use it in GitHub Desktop.
ajax requests without jquery
// POST request
var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
// GET request
var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var resp = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment