简体   繁体   中英

Why Is My catch Block Not Catching “Cross origin requests are only supported for protocol schemes: http…”

I have a JavaScript script that is using the jquery post command to log into a server and obtain a token. I've wrapped my code in a try block with a catch block that looks like this:

catch (error)
{
  alert(error.message); 
}

My code runs fine, but to test it thoroughly, I intentionally changed the protocol so that the url looks like this:

"htt://some.domain:8080/jsonrpc"

My code does not catch the error and display the alert. Rather, the Chrome console shows the following error:

XMLHttpRequest cannot load htt://some.domain:8080/jsonrpc. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

I would expect some kind of malformed url exception instead of the error in the console.

The results of other intentional errors such as incorrect password result in exceptions that are caught.

Thanks

Because Ajax is asynchronous. The error occurs outside the try/catch block.

This class of error can be detected in XMLHttpRequest by the status changing to 0 . (This status code also covers a few other error states though, so you can't be precise).

The error handler you can pass to jQuery ajax will fire when the status is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM