我正在尝试登录网站。我正在尝试使用 axios 发送 http 请求,以保存用户、名称、密码等输入的数据并将其保存到数据库中。 当我向 postman 发出请求时,它可以工作,但是当我尝试在客户端发出请求时,它不会。 我从控制台收到此错误:
Error:
("Network Error"
createError createError.js:17
handleError xhr.js:80
index.js:1375
e index.js:1375
onSubmit Register.js:39)
我尝试了各种方法,并试图在网上寻找答案,但我找不到任何东西。如果你能给我任何帮助,那就太好了。 我尝试过使用本地主机 3000,现在使用本地主机。 谢谢!!!!
代码如下
import React, { useState } from 'react';
import '../../App';
import axios from 'axios';
const Register = () => {
const [formData, gatherData] = useState({
name: '',
email: '',
password: '',
paswrd: ''
});
const { name, email, password, paswrd } = formData;
//on submit checks if passwords are valid, if they are valid create a new instance of user
const onSubmit = async element => {
element.preventDefault();
if (password !== paswrd) {
console.log("Passwords are not matching ")
}
else {
const newUser = {
name,
email,
password
}
try {
const configuration = {
headers: {
'Content-Type': 'application/json'
}
}
const body = JSON.stringify(newUser);
const res = await axios.post('http://localhost:5000/apis/user', body, configuration);
console.log(res.data);
} catch (err) {
console.error(err)
}
}
}
export default Register;
最佳答案
尝试在您的服务器上添加 cors
const cors = require('cors');
app.use(cors());
关于javascript - 尝试使用 axios 时出现网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58962755/