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 add_header
directive.
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.
Ionic 4 Http failure response for (unknown url): 0 Unknown Error. when calling api on real device
Finally, I could resolve problem after two days hardwork !
I migrated API calling from '@angular/common/http' to native http '@ionic-native/http/ngx'
with this header:
// create headers data
const headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
'Accept': 'application/json, text/plain',
"cache-control": "no-cache",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token, Accept, Authorization, X-Request-With, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Credentials" : "true",
"Access-Control-Allow-Methods" : "GET, POST, DELETE, PUT, OPTIONS, TRACE, PATCH, CONNECT",
};
The cons for me, For now on I have to test on a real device.
http failure response for 0 unknown error - Works fine in browser but fails in app
In order to solve this error, its related to splashscreen in config.xml.
There are declarations for splash density and inside it there are port-type.. and land-type.., keep everything as it is except for land declarations, every place that contains land-type.. keyword change it to just type..., for example, land-hdpi change to hdpi and for port-hdpi keep it the same without change.
<splash density="ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
<splash density="mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
<splash density="hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
<splash density="xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
<splash density="xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
<splash density="xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
<splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
<splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
<splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
<splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
<splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
<splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
<splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
<splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
<splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
<splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
<splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
<splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
And for http errors, if you are running your apidomain as http and you device is sdk version 28+ then you need to change it to https..
Http failure response for (unknown url): 0 Unknown Error,
Modify your code like below :
// in service.ts file
public createConfiguration(NewConfiguration: Configuration) { let headers = new Headers(); headers.append('Content-Type', 'application/Json'); let body = NewConfiguration.generateUrlencodedParameters(); let url = ('http://192.168.12.1/myconfig?') this.httpclient.post(url, body, {}); // <-- you need to subscribe in component.ts file } //in compoenent.ts file public callerMethod(){ //<- method which calls createConfiguration() service // srvcObj -> your service object name , paylaod -> input you are sending to servoce method this.srvcObj.createConfiguration(payload).subscribe((result:any)=>{ console.log('response create newconfiguration', result); }, (error)=>{ console.log('got error',error); } ); }
Related Topics
How to Verify Purchase for Android App in Server Side (Google Play in App Billing V3)
How to Check If Data Is Inserted in Room Database
How to Fix Mainactivity Does Not Have a Navcontroller
Error Androidmanifest.Xml Doesn't Exist or Has Incorrect Root Tag
Remove Old Fragment from Fragment Manager
How to Make a Link Open an Instagram Page in an App Installed on Android
Android Studio - Xml Editor Autocomplete Not Working With Support Libraries
How to Transfer from One Fragment to Another in Android
Photo Rotated from Camera (Samsung Device)
How to Capitalize the First Letter of Text in a Textview in an Android Application
Refresh/Reload/Re Instantiate a Fragment in an Android Tab System
Android: Keep Service Running When App Is Killed
Update Some Specific Field of an Entity in Android Room
How to Execute Code When Application Is Closing
Android Studio - Failed to Apply Plugin [Id 'Com.Android.Application']