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

183
Visualizações
Response data returns undefined from asp.net web api to vue.js

currently I'm fetching data from my api to front-end. I checked and my request body is arriving to server side. But after doing things when it comes to returning the token it always returns undefined data to vue.js:

[HttpPost("login")]
    public async Task<IActionResult> Login([FromBody]User user)
    {
        var result = await _accountRepository.LoginAsync(user.username, user.password);

        if (string.IsNullOrEmpty(result))
        {
            return Unauthorized(result);
        }

        Debug.WriteLine(result.ToString()); // this works and I can see the token
        return Ok(result);

    }

When it comes here:

  methods: {
login() {
  fetch("http://localhost:60427/api/account/login", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json', 
'Accept': 'application/json',
    },
    body: JSON.stringify({
      username: this.username,
      password: this.password,
    })
  }).then(response => {  
      console.log(response.data); // this is always undefined 
      
    })  
    .catch(e => {  
      console.log(e);  
    });  
},

}

Please help I can't see any errors here. I'm confused.

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

0

You need to call either Response.text() or Response.json() depending on what data you expect. These methods return a Promise that resolves to the data.

E.g. for JSON:

fetch("http://localhost:60427/api/account/login", {
  method: "POST",
  headers: {
    'Content-Type': 'application/json', 
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    username: this.username,
    password: this.password,
  })
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(e => console.error(e));
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