簡體   English   中英

如何檢查 email 重疊使用 Axios get and post for SignUp with React?

[英]How to check email overlap using Axios get and post for SignUp with React?

我正在嘗試過濾 SignUp email 的重疊驗證。

在我的 api.js

  const token = JSON.parse(localStorage.getItem('token'));

  const api = axios.create({
  baseURL: baseURL, // already set our base URL

  headers: {
    Authorization: `Bearer ${token}`,
    'Access-Control-Allow-Origin': '*',
  }
});

在我的 authService.js

const register = (countryCode, name, email, password) => {
  return axios
    .post('/auth/signup', {
      countryCode,
      name,
      email,
      password,
    })
    .then((response) => {
      if (response.headers.authorization) {
        console.log(response);
        localStorage.setItem('user', JSON.stringify(response.headers.authorization));
      }
      return response.headers.authorization;
    });
};

const login = (email, password) => {
  api
    .post('/auth/signin', {
      email,
      password,
    })
    .then((response) => {
      if (response.headers.authorization) {
        localStorage.setItem('user', JSON.stringify(response.headers.authorization));
      }
      return response.data;
    });
};
const checkEmail = (email) => {
  return api.get('/public/email', { email }).then((response) => {
    if (response.data.exist === true) {
      return localStorage.getItem('user', JSON.stringify(response.data));
    }
    return response.data;
  });
};

此 checkEmail 將在 SignUp.js 中

對於 onChange={emailChange}

const onChangeEmail = (e) => {
    const email = e.target.value;
    if (!email.includes('@')) {
      setEmailError('Invalid email');
    } else if (email.includes('@')) {
      setEmailError(null);
    }

    AuthService.checkEmail(email).then(
      (response) => setEmailError('Already Registered Email'),
      (error) => {
        console.log(error);
      }
    );

    setEmail(email);
  };

在這段代碼之后,

在控制台中它錯誤

Error: Request failed with status code 401
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:62)

我認為 api.get(URl, {something})

那個 {something} 是錯的,但我幾個小時都不知道......

我該怎么辦這個錯誤?

您不能在 GET 中發送正文參數,因為 POST,PUT 將起作用,使用 GET 發送,然后將數據附加到 GET url。

例如,如果您在后端使用節點服務器,那么

      api.get('/public/email/'+email).then((resp)=>{
           log(resp);
  }

使用收集數據

router.get("public/email/:youremail",(req,res)=>{
        req.param.youremail
   }

暫無
暫無

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

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