Consulting a JSON via an ANGULAR POST

Asked

Viewed 969 times

1

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

So are 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

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.

1

To send data via POST, you must use the $http 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 on this link https://docs.angularjs.org/api/ng/service/$http

0

Through the function call Controller Service, I call the function send my Factory that performs the POST q vc ta wanting.

.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;
        }
      }
   }
})

Browser other questions tagged

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