Skip to content
Advertisement

Axios call to Springboot backend responding is not forwarding to new page after returning response

My postmapping “login” in my controller is returning a 200. But I’m getting undefined and I believe it is from my axios call. I know that the undefined error is being reported from the catch block in the console

Axios call –

  submit() {
    let formData = new FormData();
    formData.set("email", this.email)
    formData.set("password", this.password)
    formData.set("staySignedIn", this.staySignedIn)

    // When the client/server sides are running from the same port (AWS) the url for this api call should be changed to /api/v1/login
    axios.post("http://localhost:8080/api/v1/login", formData,
        {headers: {'Content-Type': 'application/json'}})
        .then(function (res) {
          console.log(res); // test
          if (res.data.code === 200) {
            this.router.push('/dashboard')
            console.log("success");
          } else {
            console.log(res.data.code);
          }
        })
        .catch(function (err) {
          console.log(err);
        })
  }

Response from dev tools

Response from test

Advertisement

Answer

Axios response schema documentation is here

Unless you have a key code in your controller response, response.data.code will be undefined.

Try res.status instead if you want to check the HTTP status.

axios.post("http://localhost:8080/api/v1/login", formData,
        {headers: {'Content-Type': 'application/json'}})
        .then(function (res) {
          if (res.status === 200) {
            this.router.push('/dashboard')
            console.log("success");
          } else {
            console.log(res.status);
          }
        })
        .catch(function (err) {
          console.log(err);
        })

EDIT You seem to be sending back the password back in the response. Even though the password is encrypted, better restrict exposing it in the response.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement