reactjs 如何在取帖中使用查询字符串?

hec6srdp  于 2023-01-04  发布在  React
关注(0)|答案(1)|浏览(136)

新手在这里。建立一个React的网站。我正在努力让我的身份验证去通过获取后。它不断返回“没有这样的用户”错误,但当我验证了用户确实存在。
我在后台使用passport。

const handleSubmit = (event) => {
    event.preventDefault();

    const data = {
      Username: username,
      Password: password
    };

    fetch('https://cthulhuflix.onrender.com/login', {
      method: 'POST',
      body: new URLSearchParams(JSON.stringify(data))
    })
    .then ((response) => response.json())
    .then ((data) => {
      console.log('Login response: ', data);
      if (data.user) {
        onLoggedIn(data.user, data.token);
      } else {
        alert('Dave\'s not here! No such user.');
      }
    })
    .catch((e) => {
      alert('Something went wrong');
    });
};
ac1kyiln

ac1kyiln1#

这是我最后得到的结果:

export const LoginView = ({ onLoggedIn }) => {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  
  const handleSubmit = (event) => {
    event.preventDefault();

    const data = {
      Username: username,
      Password: password
    };

    fetch('https://cthulhuflix.onrender.com/login?' + new URLSearchParams(data).toString(), {
      method: 'POST',
      body: JSON.stringify(data)
    }).then ((response) => {
      if (response.ok) {
        onLoggedIn(username);
      } else {
        alert('Login failed');
      }
    });
  };

相关问题