简体   繁体   中英

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. When I make the request with postman it works but when I try to make the request on the client side it won't. 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. I have tried using local host 3000 and now the local. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM