1

I'm trying to use a PHP backend to retreive data from a MySQL db. Using $http.get() it works flawlessly. But when trying to post something, the php file just gets an empty array.

My controler code is as follows:

angular
.module('TestModule')
.controller('TestController', ['$scope', 'TestService',
  function ($scope, TestService) {

    TestService.testStuff().success(function(data) {
      console.log(data);
    });
  }]);

My service is:

angular
.module('TestModule')
.service('TestService', ['$http',
  function ($http) {

    this.testStuff = function () {
      var obj = {
        test: 'aaa',
        moreTest: 'bbb'
      };

      return $http.post('test-php-post.php', obj);
    };
  }]);

On the main module I set the following config:

angular
.module('TestModule')
.config(['$httpProvider',
  function($httpProvider) {
    var serialize = function(obj, prefix) {
      if(!angular.isDefined(obj)) {
        return undefined;
      }

      var str = [];
      for(var p in obj) {
        if (obj.hasOwnProperty(p)) {
          var k = prefix ? prefix + '[' + p + ']' : p, v = obj[p];
          str.push(typeof v === 'object' ?
            serialize(v, k) :
          encodeURIComponent(k) + '=' + encodeURIComponent(v));
        }
      }
      return str.join('&');
    };

    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
      return serialize(data);
    }];
  }]);

On the php file I commented everything out and left only: echo print_r($_REQUEST, 1); It just returns Array(), which is a bit strange.

I thought it might be my php configuration (I'm using grunt with php-connect) but I have tested it on my WAMP server and even on my website (which is in a commercial host, running LAMP), so I guess I can rule out PHP misconfigurations.

From the four thousand, seven hundred and twenty nine (give or take) google searches I've done (most of them bringing me back here), nothing worked. I tried to change headers, use other formats to perform the post request, $http({method:......}); for example, with no luck. I know I'm missing some really basic point but I just can't figure out what it is.

Thank you for any help you might provide.

EDIT: I did see this post and tried it. The suggested solution didn't work for me (I get an empty string as response). In fact, none of the solutions worked.. I'm going crazy with this. The content of $_POST is always an empty string. The $_REQUEST is an empty array.

I am able to make it work by encoding the data in the url such as $http.post('test-php-post.php?test=aaa&moreTest=bbb') but I will be using this to upload files and large objects. I don't think it is a good method to do so, or is it? This is basically a GET if I'm not mistaken, even if using $http.post()???

I have tried all the combinations I could remember (when everything else fails...). Tried to read the POST object with $_POST, $_REQUEST, file_get_contents("php://input")... I have set the headers to application/json or application/x-www-form-urlencoded; charset=UTF-8. Nothing works. The only way I can have data received by the php file is by encoding the object in the URL, but I can't send it in the HTTP request body.

NOTE: The serialize function in the .config works exactly as JQuery's $.param(). I tested it independantly and it worked flawlessly.

fgarci03
  • 330
  • 7
  • 15
  • if you really need to use post .... can also set default headers and use `$httpinterceptors` to modify data the same way that jQuery `$.param` works – charlietfl Jul 26 '15 at 14:38
  • Doing that currently. The data is being processed correctly but the php file just doesn't get it. Not even using postman. It is, however, the correct file, as it replies what I set it to... – fgarci03 Jul 26 '15 at 17:10
  • doing what currently? Are you using `file_get_contents()` or converting to form encoded post? – charlietfl Jul 26 '15 at 17:10
  • I've tried both methods. Currently I'm using the $httpProvider to modify the data with a vanilla JS implementation of `$.param` (also tested with the jQuery version and compared both outputs, which are the same, just to be sure). And am performing an `$http.post(url, data);` to the php file. The php file is called but it doesn't receive anything whether it's with `$_POST` or `file_get_contents('php://input')`. – fgarci03 Jul 26 '15 at 17:36
  • suggest updating question with current code attempts. Also...inspect the actual request itself in browser dev tools network tab to see exactly what is sent – charlietfl Jul 26 '15 at 17:40
  • One more question...is port and domain the same for api as page is on? In other words different server instance running for each? – charlietfl Jul 26 '15 at 17:43
  • I'm not sure. Using grunt with php-connect is new to me. All I can tell you is that I use the exact same url to access either the frontend app or the php files. In my case, `localhost:9000/path-to-files`, so I assume everything is set on the same port (and domain of course) – fgarci03 Jul 26 '15 at 17:49
  • so is what is being sent in browser what you expect by inspecting the actual request? – charlietfl Jul 26 '15 at 18:43
  • yup, the request looks to be alright... – fgarci03 Jul 26 '15 at 21:17
  • Have you checked this answer? http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload/11443066#11443066 – ssuperczynski Jul 28 '15 at 20:44
  • had not seen that specific one, but had already tried that... – fgarci03 Jul 29 '15 at 22:14

0 Answers0