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.