Fetching WordPress rest API working on Browser but not on Device (CORS Enabled)

Hey,

I’m using Ionic 4 + Angular 7 (HttpClient) to fetch JSON data from my WordPress installation. There isn’t a problem if the App runs in Browser or via --livereload. CORS also is enabled on my Server.

But if I finally build the app with Cordova (iOS), the data isn’t loading anymore. I see a http request on my server and the app seems to receive the data. But it isn’t displayed anymore. With static JSON files or with the Google Maps API everything works fine. It seems to be a problem with wordpress combined with Cordova. (Using Cordova iOS 5.0.0).

All Plugins and node modules are up to date. Whitelist Plugin is installed and configured and even the Content-Security-Policy meta tag is present.

Someone else facing this issue?

Best Regards
Marius

Hi @condacore,

Does the API work under HTTP or HTTPS? I’m thinking that if it’s HTTP, maybe Application Transport Security (ATS) is blocking these calls on iOS. But if you have followed Cordova’s iOS whitelisting steps and the server is being pinged, then it may be something else.

Best,
Rodrigo

Hey @FdezRomero,

good point! But I’m already using HTTPS. I think WordPress adds some Header Information Cordova can’t deal with. For now I’m using the native HTTP Plugin to fetch the data. Works fine so far :slight_smile:
I’m not a big fan of adding many native plugins to an ionic app, but this is the best solution for this inexplicable problem.

Best Regards
Marius

Yeah, I don’t like adding unnecessary dependencies either, but I guess you have no choice this time :sweat:

Hi,

i had the same problem and solved it with this response : https://stackoverflow.com/questions/55655100/error-on-http-requests-from-ionic-ios-native-to-wp-rest-api-works-on-web-view-a

You have to add “ionic” in protocols directly in your WP project.

Best

1 Like