10
votes

I am experiencing very strange issue when building and running ionic app. I am calling Login API from server and it work very well in browser, when run as

$ionic serve --lab

But when app is installed in Android, it gives 403 error, and does not give any other information about error code, probably, i am not sure how to get browser console log in android hybrid app.

I have login function as follows

 login: function(user) {
            var deferred = $q.defer();          
            $http.post(SERVER_URL + '/auth/login', {
                username: user.username,
                password: user.password
            }).success(function(response, status, headers, config) {

                if (response.data && response.data[0]) {
                    Auth.setToken(response.data[0].token);
                    Auth.setUserId(response.data[0].id);
                    deferred.resolve(response);
                } else {
                    deferred.reject(response);
                }
            }).error(function(response, status, headers, config) {               
                deferred.reject(response);
            });

            return deferred.promise;
        }

However, all the get request works properly.

4
Check status and headers. maybe something there - dfsq
status = 403 and headers undefined - RickDavis

4 Answers

2
votes

I ran into this recently, I couldn't POST anything from the app running on my iPhone or from the simulator without receiving a 403, but it did work directly from any browser including the one on my phone and using Postman-chrome plugin.

This seemed to be an issue with any POST request from the Ionic app: it was sending the Origin header as file://. With a Play Framework 2.4 backend, I had to disable the CORSFilter completely (removing play.http.filters from application.conf) for it to work. I'm going to investigate further and maybe provide some more details later.

  • Note - I tried a ton of configuration changes on the client/app side including Cordova whitelist plugin, X-Requested-With & Content-Type header settings, <allow-navigation href="*"\>, and nothing worked.
1
votes

The access element provides your app with access to resources on other domains in particular, it allows your app to load pages from external domains that can take over your entire webview. Just add this in config.xml

 <access origin="*" subdomains="true"/>
1
votes

It worked when i run app as

$ionic run --livereload android
1
votes

I had the same exact issue as @BatteryAcid when a Play Framework 2.4 backend. The solution was to simply add file://. in the allowedOrigins.

See the application.conf reference below

play.filters.cors {
  pathPrefixes = ["/*"]
  allowedOrigins = ["file://*", "*"]

  allowedHttpMethods = ["GET", "POST"]
  preflightMaxAge = 3 days
}

You might be able to do the same for any other backend.