Skip to content

Instantly share code, notes, and snippets.

@RolandWarburton
Created July 17, 2019 11:01
Show Gist options
  • Save RolandWarburton/676655299eed9adb348f234b241f5bce to your computer and use it in GitHub Desktop.
Save RolandWarburton/676655299eed9adb348f234b241f5bce to your computer and use it in GitHub Desktop.
xmlhttprequest simple setup
window.onload = function() {
console.log('loaded');
const url = './file.txt';
// make a new 'request'.
// request is an object so we use the keyword 'new' to make an instance of the XMLHttpRequest object
var request = new XMLHttpRequest();
// GET data from a location
request.open('GET', url);
// prevent caching so when you reload it gets the data again
request.setRequestHeader('cache-control', 'no-cache, must-revalidate, post-check=0, pre-check=0');
// what should happen when the data is loaded
request.onload = function() {
console.log(request);
document.querySelector('#test').innerHTML = request.response;
}
// it won't actually run until we tell it to
request.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment