jeudi 3 septembre 2020

Send Access Token by axios with a variable

I don't understand the variable token doesn't set data that is taken from the local storage

Error that i get is

SyntaxError: Unexpected token " in JSON at position 0 [0] at JSON.parse ()

const tokenizedCartRedirect = (history) => {
    var token  = localStorage.getItem("currentToken");

    if (token) {
      const config = {
        headers: { "Content-Type": "application/json", "x-auth-token": token },
        mode: "cors",
      };
      const body = JSON.stringify("sample data");
      const res = axios
        .post("http://localhost:5000/auth", body, config)
        .then((res) => {
          console.log("it is pushed");
          history.push("/cart");
        })
        .catch((err) => {
          history.push("/login");
        });
    } else {
      history.push("/login");
    }
  };



Aucun commentaire:

Enregistrer un commentaire