Angular request $http is not returning data

Asked

Viewed 268 times

0

Javascript code

$http({
   url: "http://app.calculadoradesementes.com.br", 
   method: "POST",
   headers: {
       "Content-Type": "application/x-www-form-urlencoded"
   },
   data: {
       nome: "Rafael", 
       email: "[email protected]", 
       telefone: "82285181"
  }
  }).success(function(data){
       console.log(data);
  }).error(function(data){
      console.log(data);
  });

Data returned

array(0) {
}

PHP file where the request takes place

var_dump($_POST);

1 answer

2

Yes, Angular’s $http service request is returning data and working as expected.

The content returned is exactly the answer of endpoint, which seems to me to be the focus of the problem:

var app = angular.module('sampleApp', []);

app.controller('SampleController', function ($scope, $http) {

  $http({
    url: "http://app.calculadoradesementes.com.br", 
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: {
      nome: "Rafael", 
      email: "[email protected]", 
      telefone: "82285181"
    }
  }).success(function(data){
    $scope.result = data;
  }).error(function(data){
    $scope.result = data;
  });
  
});
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-resource.js"></script>
<div ng-app="sampleApp">

  <div ng-controller="SampleController">
    
    {{result}}

  </div>
</div>

The result is the complete server response. Below, the return header:

HTTP/1.1 200 OK
Date: Wed, 17 Feb 2016 18:26:32 GMT
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.4.45
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: X-PINGOTHER
Access-Control-Max-Age: 1728000
Content-Length: 13
Connection: close
Content-Type: text/html

Browser other questions tagged

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