0

My loadFile function loads the content of a file from a specified file path asynchronously using XMLHttpRequest in JavaScript. It returns a Promise that resolves with the file content if the request is successful, and rejects with an empty error message if there's an error or the file is not found.

loadFile: function(filePath) {
    return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", filePath);

        xhr.onload = function() {
            if (xhr.status === 200) {
                resolve(xhr.responseText);
            } else {
                // If the file is not found, reject with an empty response.
                reject("");
            }
        };

        xhr.onerror = function() {
            // If there is an error loading the file, reject with an empty response.
            reject("");
        };

        xhr.send();
    });
}

In my case, the absence of the file I'm trying to load is a common and expected scenario, rather than the file being available. The issue I'm facing is that when the file is not found, the send() function generates a 404 error in the browser console, even though I have implemented my own onerror function to handle this.

I have also tried implementing the onloadend function, but it still doesn't prevent the 404 error from appearing in the browser console.

Is there a way to suppress the 404 errors in the browser console when the file is not found?

0 Answers0