[英]Getting a network error when trying to use axios
I am trying to make a login for a website.我正在尝试登录网站。 I am trying to send a http request with axios, to save the inputted data by the user, name, password etc and save it to a database.
我正在尝试使用 axios 发送 http 请求,以保存用户、名称、密码等输入的数据并将其保存到数据库中。 When I make the request with postman it works but when I try to make the request on the client side it won't.
当我使用 postman 发出请求时,它可以工作,但是当我尝试在客户端发出请求时,它不会。 I am getting this error from the console:
我从控制台收到此错误:
Error:
("Network Error"
createError createError.js:17
handleError xhr.js:80
index.js:1375
e index.js:1375
onSubmit Register.js:39)
I have tried various things and tried to scrape the web for the answer, but I cant find anything.If you could give me any help that would be great.我尝试了各种方法,并试图从 web 中寻找答案,但我找不到任何东西。如果你能给我任何帮助,那就太好了。 I have tried using local host 3000 and now the local.
我尝试使用本地主机 3000,现在使用本地主机。 Thanks!!!!!
谢谢!!!!!
Heres the code这是代码
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;
Try to add cors on your server尝试在您的服务器上添加 cors
const cors = require('cors');
app.use(cors());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.