1Use the Fetch API.
2Play with it at jsfiddle.net.
3
4var url = 'https://fiddle.jshell.net/robots.txt';
5var storedText;
6
7fetch(url)
8 .then(function(response) {
9 response.text().then(function(text) {
10 storedText = text;
11 done();
12 });
13 });
14
15function done() {
16 document.getElementById('log').textContent =
17 "Here's what I got! \n" + storedText;
18}
19Here's a smaller ES6 example that separates fetching from storing and showing off the result.
20
21fetch('https://fiddle.jshell.net/robots.txt')
22 .then((response) => response.text().then(yourCallback));
23
24function yourCallback( retrievedText ) { /* . . . */ }