$http.get request does not work with API level 31

633 Views Asked by At

I ask for your help for a problem that I encounter with IONIC 1. When I change the API level from 30 to 31 in the config.xml file:

this code is not working

$http.get($scope.url)
.then(function (success) {
  ...
}, function (error) {
$scope.errTXT = JSON.stringify(error);
});

returns a status of 0.

{ "data": null, "status": 0, "config": { "method": "GET", "transformRequest": [ null ], "transformResponse": [ null ], "url": "https://xxxxxxxxxxxxxxxxxx", headers: { "Accept": "application/json, text/plain, /" } }, "statusText": "" }

I don't understand why the result of http is null

Just for information, when i change back to API 30, everything is working well. I do not think is a server problem. I start thinking that the $http.get is not supported in API 31? My server is running on HTTPS://.......

 Ionic CLI         : 5.4.16 (C:xxxxxxxx\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework   : ionic1 1.3.1
   @ionic/v1-toolkit : not installed

Cordova:

   Cordova CLI       : 11.0.0
   Cordova Platforms : android 9.1.0
   Cordova Plugins   : cordova-plugin-ionic-webview 1.2.1, (and 16 other plugins)

Utility:

   cordova-res : not installed
   native-run  : not installed

System:

   Android SDK Tools : 26.1.1 (xxxxxxxx
\AppData\Local\Android\Sdk\)
   NodeJS            : v14.16.1 (C:\Program Files\nodejs\node.exe)
   npm               : 7.24.2
   OS                : Windows 10
2

There are 2 best solutions below

1
On

Did you remove the "android" folder and add it again?

ionic cordova platform remove android
ionic cordova platform add android
8
On

Status code 0 usually means a CORS error.

You can read about Ionic CORS errors here.

Essentially, it is likely a misconfiguration on your server headers which are causing your Android app to throw the status code 0.

If you are using a development environment (e.g. localhost:4200) then you can add the following to your config.xml which should allow you to run it without needing a HTTPS:

<preference name="hostname" value="localhost" />
<preference name="AndroidInsecureFileModeEnabled" value="true" />