How to retrieve sent data from a non-angular post requisition

3

I am making a post request to a php file that needs some data that is sent by the request. My question is how to retrieve this information being sent in the request

My code for the request looks like this:

//pego a descricao no compa input
var dados = {"descricao": $('#descricao').val()};

$scope.getData = function() {
    $http.post('data.php', dados).success(function(data) {
        ...
        console.log(data);
    }).error(function(data) {
        ...
        console.log(data);
    });

How would you get this information in the 'data.php' file?

var_dump($_POST); returns an empty array: array(0) { }

    
asked by anonymous 14.05.2015 / 20:54

2 answers

4

The solution is as follows:

$post = file_get_contents("php://input");
$json = json_dencode($post);
    
14.05.2015 / 21:42
2

Another output would be this:

//pego a descricao no compa input
var dados = {"descricao": $('#descricao').val()};

$scope.getData = function() {
  $http.post('data.php', dados).success(function(data) {
    ...
    console.log(data);
  }).error(function(data) {
      ...
     console.log(data);
  }).config(['$httpProvider',function($httpProvider){
    $httpProvider.defaults.transformRequest.push(
            function(data){
                var requestStr;
                if (data) {
                    data = JSON.parse(data);
                    for (var key in data) {
                        if (requestStr) {
                            requestStr += '&' + key + '=' + data[key];
                        }else{
                            requestStr = key + '=' + data[key];
                        }
                    }
                }
                return requestStr;
            }
        );
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; 
}]);
    
23.01.2017 / 19:11