9

When I use $http.get my code works, but if I use $http.post, I never get the parameters to the request .php file.

This is Service function:

    TestPanel.service('MySampleService', function ($http, $q) {
    this.getAllPosts = function () {       

        var def = $q.defer();
        $http.post('/data/AJAXRequest.php', 'mydata=1&abcd=2').success(function (data) {
            if (data == null)
                def.reject('ERROR: DATA IS NULL');
            else if (data.HasError)
                def.reject('ERROR: ' + data.Message);
            else
                def.resolve(data);
        }).error(function () {
            def.reject('ERROR: Sorry, unable to complete your request.');
        });

        return def.promise;
    }
});

And Controller function:

 TestController.controller('PostCtrl', ['$scope', '$http', 'MySampleService',
    function ($scope, $http, MySampleService) {       

        function FetchPostsList() {
            MySampleService.getAllPosts().then(function (data) {
                $scope.lstPosts = data.ResponseData;
                $scope.totalRecords = data.totalRecords;
                console.info('DATA=' + $scope.lstPosts);
            },
            function (err) {
                console.info('err=' + err);
            });
        }

        FetchPostsList();
    }
]);

and My AJAXRequest.php file

<?php 
   var_dump($_POST)
?>

if I use $http.post()

Output:

 array (size=0)
  empty

If i use $http.get() my output is :

array (size=2)
  'mydata' => string '1' (length=1)
  'abcd' => string '2' (length=1)

I checked the post in FireBug tool, that its sending data to my php file. but php file getting no params.

If I use $.ajax or $.post my code work and it gives the response.

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
Ashish Panwar
  • 1,118
  • 3
  • 11
  • 18
  • This solution worked for me http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data – Renaud Nov 19 '14 at 20:24

4 Answers4

4

What if you specify the content-type in the headers, specifically like this:

$http({
method: 'POST',
url: '/data/AJAXRequest.php',
data: { mydata: 1, abcd: 2 },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(....);

Found comments relating to PHP specifically from this question: AngularJs $http.post() does not send data

It would seem that Angular sends as application/json by default, which can confuse PHP.

Community
  • 1
  • 1
Ryan Taylor
  • 348
  • 3
  • 8
  • 1
    The way you told me its almost work....i mean, if i set parameters like this "data: { mydata: 1, abcd: 2 }" it not works. but if i use parameters "data: 'mydata=1&abcd=2'" its works fine. maybe i have to set JSON header in php file. But anyways thanks for it. – Ashish Panwar Apr 12 '14 at 20:46
  • FYI mydata=1&abcd=2 is serialized data. You can get angular 1.4 to do that automatically for you or you can use a transform request function. If find that easier so you can just use json data like JQuery. – mbokil Feb 22 '16 at 21:57
3

I faced the same issue but I found that there is no issue in angular http post method, issue is there where I am trying to get post data.

$params = json_decode(file_get_contents('php://input'),true);

I used this code to get the posted data from angular and its works like a charm

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
2

Post data like this:

$http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • 1
    i did but still not work...i dont know why php file showing en empty message. if i use $.post jquery method its work. – Ashish Panwar Apr 12 '14 at 19:11
0
$http({
    method: 'POST',
    url: 'http://mtapi.azurewebsites.net/api/sectiontype',
    dataType: 'json',
    data: {SectionTypeId:0, Name: $scope.message},
    headers: { 'Content-Type': 'application/json; charset=UTF-8' }
}).success(function (data) {
    alert(data);
}).error(function (data) {
    alert(data);
});

headers must be added in the end of the array otherwise it won't work.