1

I have this json response from a api:

[
{
"obj_Id": 66,
"obj_Nombre": "mnu_mantenimiento_de_unidades",
"obj_Descripcion": "Menu de acceso a Mantenimiento de Unidades"
},
{
"obj_Id": 67,
"obj_Nombre": "mnu_vehiculos_en_reparacion",
"obj_Descripcion": "Menu de acceso a Reparacion de Unidades"
},
{
"obj_Id": 68,
"obj_Nombre": "mnu_RTO",
"obj_Descripcion": "Menu de acceso a RTO"
}
]   

I have this function to make the request and convert the response:

async obtenerPermisos() {
      var respuesta = "";
    
      var obj = {
        idUsuario: JSON.parse(sessionStorage.getItem("Codigo")),
        contraseña: this.passwordEncriptado,
      };

      this.url = this.urlBase + "usuario/obtenerPermisos";

      respuesta = await this.$http.post(this.url, obj).then((response) => {
        const respuesta1 = JSON.parse(respuesta);
        sessionStorage.setItem("Permisos", respuesta1);
        response.data;
      });

      console.log("Los permisos del usuario son: ");
      console.log(respuesta);
      console.log("El tipo de parametro es: " + typeof respuesta);

      
      sessionStorage.setItem("Permisos", respuesta);
      this.$router.push({ name: "sideBar" });
      location.reload();
    },

but i get this error:

enter image description here

how can i resolve it?

3
  • 4
    What is response.data; doing? Are you sure you don't need to return it? Commented Jun 15, 2022 at 14:58
  • 2
    Shouldn't JSON.parse(respuesta) be JSON.parse(response)? Commented Jun 15, 2022 at 15:03
  • Barmar, it doesn't work Commented Jun 15, 2022 at 15:17

1 Answer 1

1

The data being returned is not being passed to JSON.parse()

// Wrong implementation
const respuesta1 = JSON.parse(respuesta);

// Correct implementation
const respuesta1 = JSON.parse(response.data);
Sign up to request clarification or add additional context in comments.

2 Comments

If response.data is already JSON, there's no need for JSON.parse().
yeah that's true but it's not clear from the code snippet for the question asked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.