Skip to content

Instantly share code, notes, and snippets.

@potikanond
Created March 30, 2019 08:34
Show Gist options
  • Save potikanond/5773e26f1bf6b94c2be47e64b731d998 to your computer and use it in GitHub Desktop.
Save potikanond/5773e26f1bf6b94c2be47e64b731d998 to your computer and use it in GitHub Desktop.
JavaScript "XHR" tutorial
/* References:
https://developers.google.com/web/updates/2015/03/introduction-to-fetch
https://www.w3schools.com/xml/xml_http.asp
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
*/
function reqListener() {
var data = JSON.parse(this.responseText);
console.log(data);
}
function reqError(err) {
console.log('Fetch Error :-S', err);
}
var oReq = new XMLHttpRequest();
// setting callback function for 'load' event
// fired when transaction completes successfully.
oReq.onload = reqListener;
// setting callback function for 'error' event
// fired when the request encountered an error
oReq.onerror = reqError;
// initialize request
oReq.open('get', 'https://jsonplaceholder.typicode.com/users', true);
// send async request and returns as soon as the request is send
// then wait for events
oReq.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment