Capturing exceptions from source code in Javascript?

I am trying to create my own XMLHttpRequest structure to find out how this works internally. What puzzles me is that I cannot find how to catch the exception of "the same origin."

The idea is that I am trying to load the URL, if I get the Original origin exception, I re-request the URL through the proxy script local to the script. The reason I do this is because I need to get production data from the sandbox, and I want it to be as transparent as possible for the script itself.

I know this is bad practice, but this is the least intrusive way to do it at the moment :)

Just to clean things up - I don't want to get around the same origin, I just want to catch an abandoned exception so that I can do something about it.

Here is the code I use for my xhr:

var net = function (url, cb, setts){
    this.url = url;
    this.cb = cb;

    var oThis = this;
    if (!this.xhr) {
        this.xhr = new XMLHttpRequest();
        this.xhr.onreadystatechange = function() {
            if (oThis.xhr.readyState == 4 && oThis.xhr.status == 200) {
                document.body.innerHTML += "RS: "+oThis.xhr.readyState+"; ST:"+oThis.xhr.status+"; RP:"+oThis.xhr.responseText+"<br>";
            }
            else {
                // do some other stuff :)
                document.body.innerHTML += "RS: "+oThis.xhr.readyState+"; ST:"+oThis.xhr.status+"; RP:"+oThis.xhr.responseText+"<br>";
            }
        }
    }
    this.xhr.open("GET", url,true);
    this.xhr.send();
} // It WIP so don't be scared about the unused vars or hardcoded values :)

I tried to try ... catch xhr.send (); but to no avail, they still cannot catch exceptions.

Any ideas or pointers would be greatly appreciated.

+3
source share
2 answers
xhr.onreadystatechange = function() {
    if (xhr.readyState==4) {
        if (xhr.status==0) {
            alert("denied");
        } else {
            alert("allowed");
        }
    }
}
+1
source

Are you sure that this was really an exception? I do not see anything in the specifications: http://www.w3.org/TR/XMLHttpRequest/#exceptions It seems that this is so. My bad.

, .


FWIW, jsFiddle ( -), Chrome . " ".

+1

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


All Articles