0

I have a previously working ionic app that talks to node server running socket.io server.

The socket server gets the POST but it's empty. It used to work perfectly.

I can send successfully using a REST client (PAW on mac) with no issues... the POST body is received in socket.io app.

Here's the angular code in my ionic app:

 var data = { username: 'bob', password: 'smith' }

var config = {
      headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
      }
    }
    $http.post('http://example.com:3000/login',data, config)

    .success(function(result) {
          //don't make it here

    }).error(function(data,status) {
      //make it here... no error code
    });

and in my socket.io app on the node server:

app.post('/login', function (req, res) {
  console.log(req.body); //this is empty
  console.log("req.body.username: " + req.body.username);  //empty empty
});

To confirm, the app.post does execute...se we make it that far on teh server. but the body is empty.

Preflight issue? I am stumped...need this working tonight :(

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
lilbiscuit
  • 2,109
  • 6
  • 32
  • 53
  • I should mention..this behavior occurs both with a browser (ionic serve) and also on ios device (ionic build ios --> xcode run on device) so it really can't be a CORS issue. – lilbiscuit Nov 20 '15 at 06:26
  • Progress... I changed to config to `''Content-Type': 'application/x-www-form-urlencoded'` and now I get some data on server. Console log output: `{ '{"username":"bob","password":"smith"}': '' } req.body.username: undefined` – lilbiscuit Nov 20 '15 at 06:35

2 Answers2

0

The $http.post function takes (url, data, [config]) as parameters. Reference : Documentation

By looking at your code, I think that have misplaced the parameters.

Try the following

var data = { username: 'bob', password: 'smith' }

var config = {
  headers : {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
  }
}
$http.post('http://example.com:3000/login', data, config)
.success(function(result) {
  //don't make it here

}).error(function(data,status) {
  //make it here... no error code
});
Vivek
  • 11,938
  • 19
  • 92
  • 127
0

I solved this by adding transformRequest function in the $http settings:

See this answer

Community
  • 1
  • 1
lilbiscuit
  • 2,109
  • 6
  • 32
  • 53