Response with status: 0 for URL: null. But only sometimes and only on ios


#1

Hello there,
I have strange thing going on:

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?

best regards
Skee


#2

I have the same problem, did you fix this?


#3

No, i couldnt fix it.

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);
	});
}

#4

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

See: http://ionicframework.com/docs/wkwebview/#cors

Also see: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS


#5

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


#6

In Ionic 3 iOS uses WKWebView which means that CORS is now mandatory - Android doesn’t use CORS, neither does the desktop browser

See: http://ionicframework.com/docs/wkwebview/#cors

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


#7

Actually further up the page explains it a bit better: http://ionicframework.com/docs/wkwebview/


#8

let headers: any = new Headers({
‘Content-Type’: ‘application/json’,
‘Access-Control-Allow-Origin’: ‘’,
‘Access-Control-Allow-Headers’: '
’,
‘Access-Control-Allow-Methods’: ‘*’
});
let options = { headers: headers };
this.http.post(link, myData, options)
.subscribe(data => {

}, err => {
this.loading.dismiss();
console.log(err);
alert(err);
});

still I’m facing issue in above code…please help me out from this… Thanks in advance… In android its working fine but in iOS(11.4) I’m facing

“Response with status: 0 for URL: null” issue.


#9

Hello Skee,

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

I think it is helpful you.

Thanks


#10

Solved my problem too.

I removed android 7.0.0 and installed 6.3.0.

Works fine!

Thanks