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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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

Comments

2

Post data like this:

$http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })

1 Comment

i did but still not work...i dont know why php file showing en empty message. if i use $.post jquery method its work.
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.

Comments

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.