AxiosError:XMLHttpRequest时发生网络错误,

gz5pxeao  于 2023-11-18  发布在  iOS
关注(0)|答案(1)|浏览(241)

得到AxiosError:NetworkError只有当我试图运行我的网站在我的移动的和工作完全正常的计算机
这是一个登录页面

import React, { useState } from "react";
import { Navbar } from "./header";
import axios from "axios";
export function Login(){ 
    const [data,setdata] = useState([{email:"",password:""}])
    const [mssg,setmssg] =useState("")
    function homepage(){
        window.open('/homepage','_self')
    }
  function check_user(){
axios.post('http://localhost:4000/chekuser',data)
.then(res=>{
    console.log(res.data)
    setmssg(res.data)
})
  }
const collect=(e)=>{
    setdata({...data,[e.target.name]:e.target.value})
   
}
return(
    <div>
<Navbar></Navbar>
<div id="form">
<h1 id="heading">Login</h1>
<h3 className="entries">UserName</h3>
<input name="email" onChange={collect} type="text" placeholder="Enter your email"></input>
<h3 className="entries">Password</h3>
<input name="password" onChange={collect} type="text" placeholder="Enter your Password"></input>
<button onClick={check_user}>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" className ="bi bi-arrow-right" viewBox="0 0 16 16">
  <path fillRule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</button>
<p>don't have account yet? <a href="/signin">click here</a></p>
<p style={{color:"red"}}>{mssg}</p>
</div>
    </div>
)
}

字符串
这是JavaScript中的后端与mongodb

const express =require('express')
const cors= require('cors')
const bodyParser=require('body-parser')
const mongoose=require('mongoose')
 const app=express();
 app.use(cors())
 app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
var model;
async function connection(){
    await mongoose.connect('mongodb+srv://ohyrt:[email protected]/quiet?retryWrites=true&w=majority',{useNewUrlParser:true})
    .then( console.log("connected"))
    const schema=new mongoose.Schema({
       Name:String,
        Email:String,
        Password:String,
        Username:String
    })
    
 model = mongoose.model('newuser',schema)
}

connection()
app.get('/',(req,res)=>{
    res.send("Hello world")
})
app.post('/creatuser',(req,res)=>{
 const user=new model({Name:req.body.Name,
    Email:req.body.Email,
    Password:req.body.Password,
    Username:req.body.Username })
 user.save()

})
app.post('/chekuser',async (req,res)=>{
    let email =req.body.email;
    let password = req.body.password
 model.findOne({Email:email})
 .then(user=>{
    if(user){
        if(user.Password===password){
            console.log("user found")
        }else{
            console.log("incorrect password")
            res.send("incorrect password")
        }
    }
    else{
        console.log("no user")
        res.send("user didn't exsist")
    }
 })
})
 app.listen(4000)


AxiosError:XMLHttpRequest时发生网络错误。
我得到这个错误在登录页面也我已经尝试了一些方法从stackoverflow但没有工作.
我想做一个网站,可以在手机和PC上工作,我正在使用MERN堆栈开发网站

ojsjcaue

ojsjcaue1#

在React中为你的API调用添加一个catch,它将处理你的错误并显示你得到的正确错误

axios.post('http://localhost:4000/chekuser',data)
.then(res=>{
    console.log(res.data)
    setmssg(res.data)
})
.catch((err)=>{
  console.log(err)
});

字符串

相关问题