Angular2 rxjs http.request.catch has strange behaviour for some http errors

丶灬走出姿态 提交于 2019-12-05 17:27:49

I found out the problem regards the browser and its CORS behaviour.

a) Succefull call

BROWSER                  SERVER
    | ------ preflight ---> |
    | <-- allow GET, PUT -- |
    | ------- GET req ----> |
    | <------ GET res ----- |

b) Failed call with backed response 404 instead of 200

BROWSER                SERVER
    | ---- preflight ----> |
    | <------ 404 -------- |
    |                      |
    |                      |

c) No intenet connection, no response from server

BROWSER                SERVER
    | ---- preflight ----> X
    |                      |
    |                      |
    |                      |

Whenever you make an api call to a non existing ruote, the browser sends a preflight request (b).

Depening on your backend error handling strategy you could receive:

1) Http 404 code with error details in the body In this case the browser returns a generic error with http status code 0 without the actual server's payload. The second call is never made because there is no sense in doing it since the preflight went wrong.

2) Http 200 code for the preflight and 404 for the actual call. In this case everything works fine.

The problem remains if there is no internet connection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!