Any API call returns status code 0

I’m new to Ionic and will be working on a legacy Android project. As far as I know, I installed everything I need on my machine. I can run the app, when I want to do an API call, it always returns an empty object with status 0. It doesn’t matter which API I use or which http verb. Also, on my colleague’s machine, the same code with the same API works perfectly. Here’s my code:

let options = new RequestOptions({ headers: new Headers({ 'Access-Control-Allow-Origin': '*' }), withCredentials: false});

      this.http.get('http://dummy.restapiexample.com/api/v1/employees', options2).subscribe(

        data => {

        debugger;

        var test = data;

      },

      err => {

        debugger;

        var test = err;

      });

As you can see I’m calling a dummy API and I’m setting the Access-Control-Allow-Origin header. This should simply return a json array, but instead it always falls into the err handler with a status code of 0. Is there something I´m missing? What can I do here, I’m out of options.
Thank you.

PS: here´s my ionic info:

Ionic:

   Ionic CLI          : 5.4.16 (C:\Users\Kristof\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.2.4

Cordova:

   Cordova CLI       : 8.1.1 (cordova-lib@8.1.0)
   Cordova Platforms : android 7.1.4
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.0.5, cordova-plugin-ionic-webview 1.2.1, (and 14 other plugins)

Utility:

   cordova-res : not installed
   native-run  : 1.0.0

System:

   Android SDK Tools : 26.1.1 (C:\Users\Kristof\AppData\Local\Android\SDK)
   NodeJS            : v12.16.3 (C:\Program Files\nodejs\node.exe)
   npm               : 6.14.4
   OS                : Windows 10

See if the problem persists when using https (as opposed to http) endpoints.

1 Like

I tried with an https endpoint (https://www.reddit.com/r/javascript.json) and the problem does NOT occur, instead a staus 200 is returned. Our API is using http, though and this works fine with build created on my colleague’s machine.

I’d argue that the fact that things are “working” with the build on your colleague’s machine is the bug. Underlying OSes have been getting stricter and stricter in trying to push people to use HTTPS, which IMHO is a really really good thing. Yes, if you search around (or probably even wait for other people to post in this thread) you can probably find various workarounds involving configuration settings, but I suspect you’ll just keep hitting the same wall again with new OS releases in the future, and would like to encourage you to instead secure your backend API. LetsEncrypt provides free SSL certificates. I’ve been using them since the service was in beta testing, and it’s great.

1 Like

I agree and I will definitely suggest that and push for this to be implemented soon. But for now I’ll need a workaround for this.
Btw, I somehow managed to make it work on an emulator. I have no idea what I did, though…

The problem was indeed the non-encrypted endpoint. I was able to work around it by adding the following line to my AndroidManifest.xml:

<application android:usesCleartextTraffic="true">

    </application>

Thanks for putting me on the right track!