【问题标题】:Getting a network error when trying to use axios尝试使用 axios 时出现网络错误
【发布时间】:2020-03-16 16:00:58
【问题描述】:

我正在尝试登录网站。我正在尝试使用 axios 发送 http 请求,以保存用户、名称、密码等输入的数据并将其保存到数据库中。 当我向邮递员发出请求时,它可以工作,但是当我尝试在客户端发出请求时,它不会。 我从控制台收到此错误:

 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;

【问题讨论】:

    标签: javascript node.js reactjs react-router axios


    【解决方案1】:

    尝试在你的服务器上添加 cors

    const cors = require('cors');
    app.use(cors());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2020-10-26
      • 2020-04-23
      • 2021-07-07
      • 2020-03-06
      相关资源
      最近更新 更多