Fix source-control-permission-source-code error in JavaScript

I wrote a function that keeps returning an Access-Control-Allow-Origin error. This is actually great for me; I do not want to fix this. I just want to catch him so that I can read his message in my program.

All the error code gets into my try block, and my catch block displays an error message. However, when I run the code, the error is not detected, and the error is displayed in red in the console. How can I catch this error and save her message?

        try {
            var xhr = new XMLHttpRequest();
            xhr.onload = function() {
                if (this.status < 400 && this.status >= 300) {
                    console.log('this redirects to ' + this.getResponseHeader("Location"));
                } else {
                    console.log('doesn\'t redirect');
                }
            }

            xhr.open('HEAD', $scope.suggLink, true);
            xhr.send();
        } catch(e) {
            console.log('Caught it!');
            console.log(e.message);
        }
+4
source share
1 answer

, . . Https://bugs.chromium.org/p/chromium/issues/detail?id=118096#c5:

XHR Fetch API JavaScript .

, , Fetch - , " ", - API Fetch API XHR ( XHR ). , , Fetch , " ":

- , 0, , , , .

, , , , "TypeError: Failed to fetch" - .

XHR, - onerror onerror:

xhr.onerror = function() { console.log("Error occurred but I dunno what exactly.")}
+7

Source: https://habr.com/ru/post/1672004/


All Articles