gdfgdfg gdfgdfg - 1 year ago 143
AngularJS Question

Request and response with Angular $post and PHP Codeigniter

I have a problem when I am sending data to Codeigniter via Angular $post. I am using this JS code:

$scope.user.first_name = 'first name';
$scope.user.last_name = 'last name';

method: 'POST',
url: '/registration',
data: {user: $.param($scope.user)},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}).then(function successCallback(response) {
}, function errorCallback(response) {

My PHP is based from another answer from StackOverflow, but still doesn't work:

echo file_get_contents('php://input');

, but in this way I see response empty array, if I say:


I see response NULL, Same is and with:


with or without key "user". Even if I use:


for the response I get empty array.

How to send and get data via $post Angular service with Codeigniter ?

Answer Source

If you want to send as form encoded data you need to serialize the whole data object with $.param()


 data: {user: $.param($scope.user)},


 data: $.param(angular.copy({user: $scope.user})),

Or use $httpParamSerializerJQLike

If you use defaults of $http would just be

 $ '/registration',$scope.user).then(...

And in php

$data = json_decode(file_get_contents('php://input'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download