Axios PUT和DELETE方法导致404错误(React)



我在尝试使用Axios进行PUT或DELETE请求时收到404错误。我通过删除useForm钩子来简化代码,并尝试向请求添加自定义配置,但无济于事。Axios的POST和GET请求正在工作。

import axios from "axios";
export default function Test() {
const obj = JSON.parse(localStorage.getItem('user'));
const email = obj[0].email;
const firstName = obj[0].firstName;
const lastName = obj[0].lastName;
const password = obj[0].password;
const dateOfBirth = obj[0].dateOfBirth;
const gender = obj[0].gender;
const id = obj[0].id;
const URL = "http://localhost:5000/users?email=" + email;
const onSubmit = (e) => {
e.preventDefault();
axios.delete(URL, {data:{
firstName: firstName, 
lastName: lastName, 
email: email, 
password: password, 
gender: gender,
dateOfBirth: dateOfBirth,
id: id,
}, headers:{Authorization: "token"}})
const response = axios.get(URL)
return (response.data)
}
return (
<form onSubmit={onSubmit}>
<button type="submit">DELETE ACCOUNT</button>
</form>
)
}

我正在使用axios 0.27.2版本和react 18.0版本。

我安装了Postman,并尝试了GET、POST和PUT请求。

虽然我可以从"http://localhost:5000/users?email={email}"我无法在没有收到未找到的404错误的情况下进行PUT或DELETE请求。

我把URL改为"http://localhost:5000/users/{id}"并且不再收到GET、PUT或DELETE的任何错误。我没有尝试过所有的方法,但是,我相信id可能是最好的方法。

我仍然不知道为什么使用键会导致404错误。

最新更新