user3522457
user3522457

Reputation: 2963

$http angularjs POST don't send my data?

my angularjs $http

userId = '1';

$http({
url: "php/loadTab.php",
method: "POST",
data: userId
}).success(function(data, status, headers, config) {
    console.log(data);
}).error(function(data, status, headers, config) {
});

heck, POST doesn't send the data to my php. I do echo $_POST['userId'] it returned undefined index. I also tried data:'userId':'1'

Upvotes: 2

Views: 432

Answers (1)

MrCode
MrCode

Reputation: 64526

Angular sends the data as JSON, not form encoded key/value pairs so you can set data to an object. You almost have it on your second try but you need to include the curly braces, which define an object:

data: { 'userId' : userId }

Now, on the PHP side you need to access the raw POST data to decode the JSON like so:

$data = json_decode(file_get_contents('php://input'));
echo $data->userId;

Upvotes: 2

Related Questions