Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
how to get a response from a post request

I have a post request. It is made and completed successfully, but I would like to know how I could use console.log(res.valor) in it, follows the data

consultarCorreio(){
  this.cartS.cart.valorTotalItems
  this.cartS.getCorreiosPrazoPreco(this.cartS.cart.pesoTotal, 80220060, this.formGeral.value.entrega_cep).subscribe((res: any) => {
    if (res.error) {
      this.modalS.createModal(MdAlertComponent, { title: 'Erro ao obter dados do período', message: 'Recarrega a página e tente novamente ou espere alguns minutos. Se o erro persistir, entre em contato com a nossa equipe.' });
      return;
    }
    console.log(res.data.valor)
    this.freteCorreio = res.data.valor
    console.log(this.freteCorreio)
  });
}

Returns this json (res)

{
"status": true,
"data": {
"cServico": {
  "Codigo": "04510",
  "Valor": "65,00",
  "PrazoEntrega": "6",
  "ValorSemAdicionais": "65,00",
  "ValorMaoPropria": "0,00",
  "ValorAvisoRecebimento": "0,00",
  "ValorValorDeclarado": "0,00",
  "EntregaDomiciliar": "S",
  "EntregaSabado": "N",
    "obsFim": {},
  "Erro": "0",
    "MsgErro": {}
   }
 }
}

How can I use console.log(res.Valor) to get the value of "Valor" (65.00) I tried using console.log(res.value) but it returned undefined.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're getting undefined for console.log(res.value) because as you can see, your response doesn't have a value field. To get the value of field Valor use console.log(res.data.cServico.Valor). (that's because you have an object cServico embedded in the data object from your response).

about 4 years ago · Juan Pablo Isaza Denunciar

0

So, I noticed some of your code is typescript, but I'm going to answer your question about console.log and the res object in javascript.

The following code assumes that your JSON response is exactly as you posted in your question and as follows:

{
  "status": true,
  "data": {
    "cServico": {
      "Codigo": "04510",
      "Valor": "65,00",
      "PrazoEntrega": "6",
      "ValorSemAdicionais": "65,00",
      "ValorMaoPropria": "0,00",
      "ValorAvisoRecebimento": "0,00",
      "ValorValorDeclarado": "0,00",
      "EntregaDomiciliar": "S",
      "EntregaSabado": "N",
      "obsFim": {},
      "Erro": "0",
      "MsgErro": {},
    },
  },
}

In this case, getting the Valor property in this would require accessing res.data.cServico.Valor. If you're receiving undefined, then I have to assume that these properties do not exist in your test (which means I'm not able to reproduce the problem).

This example can be seen in the following snippet:

const res = {
  "status": true,
  "data": {
    "cServico": {
      "Codigo": "04510",
      "Valor": "65,00",
      "PrazoEntrega": "6",
      "ValorSemAdicionais": "65,00",
      "ValorMaoPropria": "0,00",
      "ValorAvisoRecebimento": "0,00",
      "ValorValorDeclarado": "0,00",
      "EntregaDomiciliar": "S",
      "EntregaSabado": "N",
      "obsFim": {},
      "Erro": "0",
      "MsgErro": {},
    },
  },
};
console.log(res.data.cServico.Valor);

If you receive undefined, I would suggest checking the parent parameters until you get something other than undefined to trace the source of your error.

console.log(res.data.cServico);
console.log(res.data);
console.log(res);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda