Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$http.post does not work in ionic android app

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.

like image 881
RickDavis Avatar asked Dec 23 '14 11:12

RickDavis


3 Answers

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.
like image 171
BatteryAcid Avatar answered Oct 04 '22 08:10

BatteryAcid


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"/>
like image 33
Kashif Avatar answered Oct 04 '22 10:10

Kashif


It worked when i run app as

$ionic run --livereload android
like image 34
RickDavis Avatar answered Oct 04 '22 10:10

RickDavis