简体   繁体   English

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

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

I'm trying to filter overlap validation for SignUp email.我正在尝试过滤 SignUp email 的重叠验证。

in my api.js在我的 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': '*',
  }
});

and in my authService.js在我的 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;
  });
};

This checkEmail will be in the SignUp.js此 checkEmail 将在 SignUp.js 中

for onChange={emailChange}对于 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);
  };

after this code,在这段代码之后,

in the console it error在控制台中它错误

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

I think inn the api.get(URl, {something})我认为 api.get(URl, {something})

that {something} is wrong but I don't have any idea for hours...那个 {something} 是错的,但我几个小时都不知道......

what can I do for this error??我该怎么办这个错误?

you can't send body parameter in GET, for that POST,PUT will work, to send with GET then attach data to the GET url.您不能在 GET 中发送正文参数,因为 POST,PUT 将起作用,使用 GET 发送,然后将数据附加到 GET url。

example if your using node server at backend then例如,如果您在后端使用节点服务器,那么

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

collect the data using使用收集数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM