I have an HTML file ( suppose "myHTML.html") and a local text file from which I want to load content and attach it to myHTML.
My problem is that whenever I try to use this code:
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
which I've seen in a couple of threads here to read locally from text file, it works fine with VS-code and liveserver, but when I open myHtml directly with chrome without VS-code I get errors that say:
"Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load",
"Access to XMLHttpRequest from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https"
"Failed to load resource: net::ERR_FAILED"
Does anyone have any solution to this?