I get "Http failure response for (unknown url): 0 Unknown Error" instead of actual error message in Angular
The problem was related to CORS. I noticed that there was another error in Chrome console:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 422.`
This means the response from backend server was missing
Access-Control-Allow-Origin header even though backend nginx was configured to add those headers to the responses with
However, this directive only adds headers when response code is 20X or 30X. On error responses the headers were missing. I needed to use
always parameter to make sure header is added regardless of the response code:
add_header 'Access-Control-Allow-Origin' 'http://localhost:4200' always;
Once the backend was correctly configured I could access actual error message in Angular code.
In case anyone else ends up as lost as I was... My issues were NOT due to CORS (I have full control of the server(s) and CORS was configured correctly!).
My issue was because I am using Android platform level 28 which disables cleartext network communications by default and I was trying to develop the app which points at my laptop's IP (which is running the API server). The API base URL is something like http://[LAPTOP_IP]:8081. Since it's not https, android webview completely blocks the network xfer between the phone/emulator and the server on my laptop. In order to fix this:
Add a network security config
New file in project: resources/android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <!-- Set application-wide security config --> <base-config cleartextTrafficPermitted="true"/> </network-security-config>
NOTE: This should be used carefully as it will allow all cleartext from your app (nothing forced to use https). You can restrict it further if you wish.
Reference the config in main config.xml
<platform name="android"> ... <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:networkSecurityConfig="@xml/network_security_config" /> </edit-config> <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" /> .... </platform>
That's it! From there I rebuilt the APK and the app was now able to communicate from both the emulator and phone.
More info on network sec: https://developer.android.com/training/articles/security-config.html#CleartextTrafficPermitted
working for me after turn off ads block extension in chrome, this error sometime appear because something that block http in browser