Is it possible to intercept the response to the HTTP OPTIONS preposition in AngularJS?

I am trying to implement a simple interceptor that allows me to display a message in the lines "cannot connect to server" in my Angular application. However, since the API is on a different host, I deal with CORS requests before flying OPTIONS .

I found that if the API is not available, the Chrome developer tools show 503 in the OPTIONS request, but the Angular $http interceptor gets a 404 response to the next GET request. I believe this is because the OPTIONS response did not contain the required CORS headers, so the GET actually never ran.

Is it possible to intercept the OPTIONS answer? If all I see is 404 , I cannot distinguish "server down" from "no such resource."

+7
angularjs cors
source share
1 answer

You cannot intercept this design request - the browser "checks" for you, making sure that you are allowed to make the request.

We used three solutions for this:

  • If the problem is that you are using a development environment such as NodeJS and your domain names do not match (that is, if you usually do not need to do this in Production), you can use a proxy server. https://github.com/substack/bouncy The BounceJS NodeJS module is an easy-to-use option. Then your web service request domain will correspond to the domain on which your page is located, and verification will not be initiated. (You can also use tricks like this in Production, although it can be easily abused!)
  • Also for temporary use, you can use something like Fiddler or Charles to manipulate the request, spoofing the necessary headers or asking the browser not to check them (--disable-web-security in Chrome).
  • If you have this problem in Production, you either need to fix it legally (configure the web service handler to add the required headers - there are only two of them), or find a way to make the request in such a way, t run the check. For example, if you manage both the source and destination domains, you can put the script in a target that makes requests to itself. Run it in IFRAME, invisibly. Then you can use things like postMessage () to talk back and forth. Larger services such as Facebook use XHR bridges, for the same reason.
+3
source

All Articles