Pass data via POST in AngularJS to PHP API

2

Good afternoon

I need to pass data via post using angularjs to an api php data appear in the controller but not in api PHP.

if( isset($_REQUEST['mes']) ) {
    $mes = $_REQUEST['mes'];
} else {
    $mes = date('m');
}

In the controller I visualize the form data (mes and origin):

.controller('AcpBuscaCtrl', function($scope, ApiAcp, $ionicLoading, $timeout, $http, $q) {  
  $scope.pesquisaAcp = function(data ) {
    $scope.mes = data.mes;
    $scope.origem = data.origem;
    $ionicLoading.show({
      noBackdrop: false,
      template: '<p>Pesquisando dados! aguarde ...</p>'
    });
    var q = $q.defer();
    $http.get(ApiAcpEndpoint.url, data)
      .then(function(data) {      
         console.log(' - data.mes '+$scope.mes);
         console.log(' - data.origem '+$scope.origem);

          var acompanhamento = {};
          acompanhamento.dados = [ data ];
          $scope.data = acompanhamento.dados;
          console.log('pesquisaAcompanhamento data :'+$scope.data);

          q.resolve(data);
          $ionicLoading.hide();

          console.log(' q.promise '+q.promise);
          return q.promise;
      });
  }
})

And in services too the search result does not return:

.factory('ApiAcp', function($http, $q, ApiAcpEndpoint) {
    var getApiData = function(data) {    
    var q = $q.defer();    
    $http.get(ApiAcpEndpoint.url,data)
    .success(function(data) {
      q.resolve(data);
    })
    .error(function(error){
      console.log('Had an error'+error)
      q.reject(error);
    })
    return q.promise;
  }
  return {
    getApiData: getApiData    
  };
})
    
asked by anonymous 14.07.2015 / 20:00

2 answers

1

For reasons that until today I do not know what they are, when angular passes a post pro php you can not catch in the traditional way, try the following way:

$data = json_decode(file_get_contents("php://input"));

Source: link

    
06.12.2016 / 18:25
0

Good for your code you are not doing a post but a get ... both in the controller and in the factory, replace as below and test your code ... ABÇ!

$http.post(ApiAcpEndpoint.url, data)
      .then(function(data) {      
         console.log(' - data.mes '+$scope.mes);
         console.log(' - data.origem '+$scope.origem);

          var acompanhamento = {};
          acompanhamento.dados = [ data ];
          $scope.data = acompanhamento.dados;
          console.log('pesquisaAcompanhamento data :'+$scope.data);

          q.resolve(data);
          $ionicLoading.hide();

          console.log(' q.promise '+q.promise);
          return q.promise;
      });

Factory:

$http.post(ApiAcpEndpoint.url,data)
    .success(function(data) {
      q.resolve(data);
    })
    .error(function(error){
      console.log('Had an error'+error)
      q.reject(error);
    })

Problem that if you execute a "get" the parameters do not really follow "$ http.get (linkAPI, parameters)" and yes $ http.get (linkAPI +? + parameters) ... ABÇ!

    
14.07.2015 / 20:16