Querying a JSON via a POST on ANGULAR

1

I have an app in IONIC which should consume a JSON via POST, so I passed through POSTMAN I have a URL, the version and a token defined as I do to pass this via Angular, and return my JSON .

So there's my data I have in POSTMAN

Key
Value
Description
Bulk Edit
versao
1.1

consulta 
{"data":"2017-04-03","data":"2017-04-03"}

token
xxxxxxxxxxxxxxxxxxxx
    
asked by anonymous 12.11.2017 / 19:19

3 answers

2

I do so, using $ http with PUTS.

.service('LoginService', ['$http',
  function($http) {

    this.getToken = function(login, senha) {
      return $http({
        method: 'PUT',
        url: "https://url.do.site.com/usuarios/token/acesso",
        headers: {
          "Content-Type": "application/vnd.api+json",
          "Accept": "*/*",
          "Usuario-Login": login,
          "Usuario-Senha": senha
        }
      }).then(function(data){
        return data;
      }).catch(function(error){
        console.log(error);
      })
    }
  }
])

or so using POST

$http({
  method: 'POST',
  url: 'https://url.do.site.com/usuarios/novo/usuario',
  params: {
    email: usuario.email,
    nome_usuario: usuario.nome_usuario,
    patrocinador: usuario.associado,
    nome: usuario.nome,
    cpf: usuario.cpf
  },
  headers: {
    "Content-Type": "application/vnd.api+json",
    "accept": "*/*"
  }
}).then(function(data){
  console.log(data);
}).catch(function(error){
  console.log(error);
})

It will depend a lot on how the API wants to receive the data.

    
13.11.2017 / 03:31
1

To send data via POST , you must use $http of AngularJS.

Example:

var data = {"data":"2017-04-03","data":"2017-04-03"}
$http.post('/suaURL', data).then(successCallback, function errorCallback(){});

function successCallback(result){
    alert('dados enviado com sucesso);
}

You can see more details at this link link $ http

    
12.11.2017 / 21:20
0

Through the function call of the controller, I call the function sendInformations from my factory that performs the POST that you want.

.controller('meuController', function ($scope, meuServico) {
  $scope.chamarServico = function (parametros) {
   meuServico.enviarInformacoes(parametros);
 }
})

.factory('meuServico', function ($http) {
   return {
      enviarInformacoes: function (parametros) {
        return $http.post('https://www.minhaurl.com', parametros).then(function (response) {
           return response.data;
        }
      }
   }
})
    
20.11.2017 / 13:40