Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Cannot set results of an api call to a variable in an Angular 1 application

I am trying to call an api, and set the results to a variable. I am able to successfully make the call and see the data when I log it in the console, but later when I try to use the variable it is undefined.

Here is the relevant code:

        var postParams = {
            "code": input.Code,
            "number": input.Number
        }

        var result;

        try {
            $http.post("/api/DetailAPI/GetDetails?code=" + input.Code + "&number=" + input.Number, postParams).success(function (details) {
                console.log(details);
                result = JSON.parse(details);
            });
        }
        catch (error) {
            console.log(error);
        }

        console.log(result);

The first console.log(details) shows the data I am expecting listed as "Object". When console.log(result) is called result is undefined.

I have tried with and without JSON.parse() and get the same result either way.

I know the api call is working, since I can see the data in the console. The issue is that I cannot store this data in my result variable.

How do I get Angular to actually put the data I get from the api call into a variable?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are consoling the result variable before it has been defined.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda