Pass data via POST in Angularjs to PHP API

Asked

Viewed 2,167 times

2

Good afternoon

I need to pass data via post using Angularjs for a php api the data appears in the controller but not in the PHP api.

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

In the controller I visualize the form data (month 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 also however 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    
  };
})

2 answers

1


  • 1

    This happens because inputs are sent via body in json format.

0

Good for your code you are not performing a post but a get... in both controller and Factory, replace 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 run a "get" the parameters do not actually follow "$http.get(linkAPI, parameters)" and yes $http.get(linkAPI+?+parameters)... ABÇ!

  • I made a mistake and posted a test that I had done with get, this code with post does not deliver the data to the php api, even if the.log console data arrives in the controller

Browser other questions tagged

You are not signed in. Login or sign up in order to post.