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.

11
  • 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 Commented Jul 26, 2015 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... Commented Jul 26, 2015 at 17:10
  • doing what currently? Are you using file_get_contents() or converting to form encoded post? Commented Jul 26, 2015 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'). Commented Jul 26, 2015 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 Commented Jul 26, 2015 at 17:40

0

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.