If i run my app on an iOS device, the http requests dont work correctly.
Round about 70% of these requests are failing with the error “Response with status: 0 for URL: null”.
But if i repeat them a few times, at one point, they just work. But the next time, they fail again. So i try them a few tmies again and then it works again.
On browser and android, everything works just fine.
Does anyone have an idea, why this problems occur?
So i created a workaround:
If my http request fails, i just try it again and again (5 times max) until it it works.
Here a short snippet of my code, i hope, it helps somehow:
// This is the standard http/post function used systemwide
post(endpoint, payload, add_device_infos=false):Promise<any>{
//create a unique hash for every request that comes in and save it in an array with its counter at value 1
let requestHash=this.getHash();
this.requests[requestHash]=1;
//send the request
return new Promise((resolve, reject) => {
this.http.post(this.config.api_url+''+endpoint,payload,{}).map(res => res.json()).subscribe(data => {
//request successfull, resolve the promise
resolve(data);
},
err => {
//Request fails, so i handle the failure
this.postFailure(endpoint, payload, requestHash, resolve, reject);
});
});
}
//handle the failure
postFailure(endpoint, payload, requestHash, resolve, reject){
//if the request hash doesnt exist or the counter for this request hash reached 5, i stop trying and accept, that the request failed for good
if(!this.requests[requestHash] || this.requests[requestHash]>=5){
delete this.requests[requestHash];
this.loader.stopLoading();
this.toaster.present(this.getTranslation('connection_error'));
reject({});
return;
}
//Increase the counter for this request hash and try again
this.requests[requestHash]++;
this.http.post(this.config.api_url+''+endpoint,payload,{}).map(res => res.json()).subscribe(data => {
//request successfull, resolve the promise
resolve(data);
},
err => {
//Request fails, so i handle the failure
this.postFailure(endpoint, payload, requestHash, resolve, reject);
});
}
It will be a CORS issue - look at the headers for the requests - the server needs to allow certain headers, also note that there is a preflight request - see the OPTIONS request
But if it is a CORS issue, it wouldnt work at all. But it works flawless on Android and the browser.
And on iOS it sometimes works on the first try and sometimes on the third try (same request).
So i dont see, why this is a CORS issue…
But if you could convince me, i would be more then happy to have at least any clue
WKWebview was definitely the right way to go with Ionic 3 (although more difficult to use because of CORS) as Apple recommend that you don’t use the older UIWebView which was used in Ionic 1 - see: https://developer.apple.com/documentation/webkit/wkwebview
I have same issue in android, rest api not working in android emulator or device and give error: Response with 0 for Url: null.
and finally i got solution that there is cordova platform android version issue, if there is cordova Android version is 6.3 then it’s working properly but it there is cordova Android version is 7.0 or 7.1.1 then it’s not working for me.
So, I think in ios you should check your ios platform version and then try again.
You can check ios or android version using cli command. ionic info
Also check your all cordova plugins is installed or not. If someone is missing then install it manually and check again.
You can check your cordova plugins using cli command. cordova plugin -ls
Hi EveryOne I have found the best possible solution for WKWebView and it works fine
This issue happened due to the WKWebView Mostly in IONIC 3
So, the very simple way to use the WKWebView in Ionic is that You Must uninstall the previously installed UIWebView from the plugin by running the following command.
After doing all that when you try to run the application and hit some api call you will get the preflight issue in that due to CORS so to fix that. Simply run the following command