Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Created August 24, 2016 20:36
Show Gist options
  • Save qetr1ck-op/f52380392d7f0afb4835f8257a483ff7 to your computer and use it in GitHub Desktop.
Save qetr1ck-op/f52380392d7f0afb4835f8257a483ff7 to your computer and use it in GitHub Desktop.
var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/url'); //xhr.open(method, URL, async, user, password)
xhr.send(); //xhr.send([body])
//xhr.abort() to cancel the request
//also more convenience way to use
//onload onerror events
xhr.onreadystatechange = function() {
/*
const unsigned short UNSENT = 0; // initial state
const unsigned short OPENED = 1; // open is triggered
const unsigned short HEADERS_RECEIVED = 2; // receive headers
const unsigned short LOADING = 3; // still is receiving packages
const unsigned short DONE = 4; // request is done
*/
if (this.readyState != 4) return;
// after done, are available:
// status, statusText
// responseText, responseXML (when content-type: text/xml)
if (this.status != 200) {
// handle error
alert( 'err: ' + (this.status ? this.statusText : 'something is wrong') );
return;
}
// get result from this.responseText or this.responseXML
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment