I created a sample text file called sample.txt with a line of text like this:
I am a sample text file
And then in the same Visual Studio Code, I have the API.html file:
var textButton = document.getElementById('getText');
textButton.addEventListener('click', getText);
function getText() {
fetch('sample.txt')
// Fetch returns a promise
.then(function(res) {
console.log(res);
})
}
When I run it, the browser said paused on promise rejection.
Type Error: Failed to fetch