0

I'm working on my first android app in Ionic framework. My app should communicate with a REST interface from AngularJS. When I'm testing the app in a browser on my pc then everything works just fine but when I'm running it as an android app from a virtual or a real android device then the POST request doesn't work at all. The GET requests work perfectly. I don't have a clue why it's happening so I appreciate any help!

Part of the code:

    $http({
        url: address,
        method: "POST",
        data: jsondata,
        transformRequest: false,
        headers: { 'Content-Type': undefined }
      })
      .success(function (data) {
         $scope.errormsg = "success";
         reloadComments();
      })
      .error(function (data) {
         $scope.errormsg = data;
      });
6
  • show use the code of whats not working? Commented Aug 5, 2015 at 17:55
  • @Arlind : I pasted in a part of the code but as I said the same code works in the browser but not when I run the app on a device. Commented Aug 5, 2015 at 18:02
  • How are you loading the code in Android? Commented Aug 5, 2015 at 18:19
  • @Arlind : How do you mean? Commented Aug 5, 2015 at 18:23
  • How are you running the code in android Commented Aug 5, 2015 at 18:24

2 Answers 2

1

If you're using a newer version of Cordova (or the latest Ionic CLI) to develop your app, you may be experiencing http 404 errors when your app tries to make network requests.

This can be solved quickly with the Cordova whitelist plugin! To install it, just run the following in your project's directory:


ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git


Refer this Link For More Details: http://docs.ionic.io/docs/cordova-whitelist

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I realized it was a CORS problem.
0

You have to convert all parameters in object then you have to call web services like following example.

$scope.submitForm = function($valid)
{
    myobject = {'email' : $scope.user.email, 'pass' : $scope.user.pass};
    Object.toparams = function ObjecttoParams(obj)
    {
        var p =[];
        for (var key in obj)
        {
            p.push(key + '=' + encodeURIComponent(obj[key]));
        }
        return p.join('&');
    };

    $http({
        url: WSURL + '/login',
        method: "POST",
        data: Object.toparams(myobject),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data, status, headers, config)
    {

    }).error(function (data, status, headers, config) 
    {

    });
}

I hope its help to you

6 Comments

I tried your code. With my code it happens absolutely nothing, with yours it runs the error method at least but nothing goes to the server. I also tried with simple Javascript code. It happens the same like with your code. I don't understand why GET works and POST not.
I don't have a console because I run the app on a device. (I don't know if there is a way to have a console on a device, haven't tried yet) But with your code I get status code 403.
If you do with ionic try with ionic serve first then try on device
It works fine with ionic serve. I just installed wireshark on my pc and I realized that the POST is coming from the app with any code. So it's the server (I'm using play framework) which forbids the connection.
Just try on your mobile if you have
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.