LoginSignup
3
0

More than 5 years have passed since last update.

XMLHttpRequestでCORSエラーとネットワークエラーを区別する方法は存在しない

Last updated at Posted at 2018-09-06

XMLHttpRequestでリクエストを発行してエラーになったとき、

  1. CORSエラー(Access-Control-Allow-Origin ヘッダが存在しないなど)
  2. ネットワークエラー(TCPコネクションを確立できないなど)

上記のどちらが原因なのかをプログラム的に判定する方法は存在しない。

  • どちらも xhr.readyState==4 かつ xhr.status==0 になる
  • CORSの仕様的に、どちらも同じふるまいをするように規定されているらしい
  • Chrome Devtoolsのコンソールには No 'Access-Control-Allow-Origin' header is present on the requested resource. というエラーが表示されるんだけど、この文言をプログラム的に取得する方法はない

参考:
ajax - How to Detect Cross Origin (CORS) Error vs. Other Types of Errors for XMLHttpRequest() in Javascript - Stack Overflow

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0