HTTP request issue on IOS : Always return null

I have some problems with $http.get calls in ionic 1 app. This is the code i have :

$http.get('my.api.com', { timeout: 30000 }).then(function(response) {
console.log("fresh", JSON.stringify(response))
}, function(error) {
console.log(error); 
})

I’ve added this in my index.html :

<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval'">

and this to my config.xml file:

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>

This call works well on Android (device and emulator), work on all browsers (chrome, firefox and safari), but return null in iOS device. What can be the issue?
Note: my.api.com points to a self made jax-rs 2.0 api and there is no error. I have also added cordova whitelist plugin
I have a high value of timeout because i wanted to see if the request was timed out before the response was present
Thank you !!!

You might need to add some settings to the IOS app .plist. By default iOS (since iOS 9) can now only communicate via https due to Application Transport Security. In the future it will be mandatory for all apps in store.

What am i supposed to add ?

Doesn’t work. The request ends with status 0 data null

do you get any console errors on xcode console?

No error in console… it returns an object with status:0 and data:null.

I suggest a dumb idea, because I had that a lot while coding this week, async issue?

I never used ionic v1, but you can try to define the value of #item in template view to null or ‘’ in the controller, before the template try to access/change it?

page.ts

market:  any = '';

// .. before the

constructor{
things..
}

this is the log i’m getting:

Object
config: Object
headers: {Accept: "application/json, text/plain, */*"}
method: "GET"
timeout: 25000
transformRequest: [function] (1)
transformResponse: [function] (1)
url: "my.api.com"
Prototype Object
data: null
headers: function(name)
status: 0
statusText: ""
Prototype Object

What do you have as input from API if you do a map=>res(json) or stg like that? Looks like you get an object that is not “unwrapped”. And if you get an “unfolded/unwrapped” object, it will forever display “undefined” in template.

Are you sure the API URL is reachable in the network

This is the code i have in the service (called provider in V2)

$http.get('my.api.com', { timeout: 30000 }).then(function(response) {
console.log("fresh", JSON.stringify(response))
}, function(error) {
console.log(error); 
})

And the line console.log(error) produces this object as error:

Object
config: Object
headers: {Accept: "application/json, text/plain, */*"}
method: "GET"
timeout: 25000
transformRequest: [function] (1)
transformResponse: [function] (1)
url: "my.api.com"
Prototype Object
data: null
headers: function(name)
status: 0
statusText: ""
Prototype Object

That same code works on Android (Simulator and device perfectly), even in browser.

As input from my API I have:

serviceFactory.getPdvs().then(function(markers)

where service factory is the provider.

Yes. As it prints the error i copy the url and test in safari browser, it works well, i have the same project in an Android device, and it passes well, I have the good result. But in IOS it doesn’t work.

Nothing to do with a CORS issue specific to Safari? No HTTP error code like forbidden? (403) Is your app in production and validated on one app store not the other?

No, it prints the good result in the browser. I’ve read about ATS security applied in ios10 which can be the source of this behavior and I don’t know how to apply to my case as i have a public IP address with port.

Ah! I read something about that for ionic native and ios9/10 fix… this was on the forum several monthes ago, but which topic no idea sorry :confused: If I remember, it said yeah that since ios9/10 CORS has been reinforced, dig a bit maybe.

Thank you for your help, i will continue research

CORS issue should not happen on Device.
Add

 <allow-navigation href="*" />

on your config.xml and see if its working.

Note: Make sure cordova whitelist plugin is installed.

cordova whitelist works only on Android.

To solve this issue i finally installed a certificate to have https request. Everything works well now.

1 Like