簡體   English   中英

修改 localStorage 出現跨域錯誤

[英]Modifying localStorage gives A cross-origin error

我正在使用 reactjs 制作 web 應用程序,當開發時突然出現此錯誤:“引發了跨域錯誤。React 無法訪問實際錯誤 ZA8CFDE6331BD59EB2AC96F8.IZAuth4 開發中的 user666F8.IZAuth4 巫婆”本地存儲:

 const AuthProvider = ({ children }) => {
    
      const token = localStorage.getItem("token");
      const userInfo = localStorage.getItem("userInfo");
      const expiresAt = localStorage.getItem("expiresAt");
    
      const [authState, setAuthState] = useState({
        token,
        expiresAt,
        userInfo: userInfo ? JSON.parse(userInfo) : {},
      });
    
      const setAuthInfo = ({ token, userInfo, expiresAt }) => {
        localStorage.setItem("token", token);
        localStorage.setItem("userInfo", JSON.stringify(userInfo));
        localStorage.setItem("expiresAt", expiresAt);
    
        setAuthState({
          token,
          userInfo,
          expiresAt,
        });
      };
    
     

不幸的是,我在更新用戶詳細信息時遇到了問題。 在上下文中,我制作了另一個 function:

編輯:我忘了寫在 function 執行后 userDetails 更改為 undefined 所以我得到一個跨域錯誤

const setUserInfo = ({  userInfo }) => {
        localStorage.removeItem("userInfo");
        localStorage.setItem("userInfo", JSON.stringify(userInfo));
        

        setAuthState({
          userInfo,
        });
      };

我有更新詳細信息的 rest-api 路由,示例性響應:

{
    "success": true,
    "user": {
        "role": role,
        "_id": id,
        "username": name,
        "email": email,
        "createdAd": createdAt,
        "__v": 0
    }
}

和用戶登錄時的用戶rest-api響應,例如:

{
    "success": true,
    "token": token,
    "expiresAt": expiresAt,
    "userInfo": {
        "role": role,
        "_id": id,
        "username": name,
        "email": email,
        "_id": id
    }
}

當我調用它但在我得到跨站點錯誤之后更新詳細信息。

在這里提交 function

const handleUpdate = async (info) => {
    setError("");
    setLoading(true);

    try {
      const { data } = await fetchContext.authAxios.put(
        "http://localhost:5000/api/v1/auth/update-details",
        info
      );
      const user = data.user;
      console.log(user);
      auth.setUserState(data);
      setError(null);

      setLoading(false);
    } catch (err) {
      setLoading(false);
      // const { data } = err.response;
      // setError(data.error);
      console.log(err);
    }
  };

好吧,我笨

const setUserInfo = ({  userInfo }) => {};

應該

const setUserInfo = ( userInfo ) => {};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM