Axios给出404错误,而不是插入数据库

发布于 2025-02-13 20:02:41 字数 3209 浏览 0 评论 0原文

我基本上使用React创建了一个简单的UI,从那里开始,我将用户输入以一种形式输入,并使用Axios将其发送到后端。 这是前端代码:

import {useState} from "react";
import Axios from "axios"
function App() {

  const [name, setName] = useState("");
  const [age, setAge] = useState(0);
  const [country, setCountry] = useState("");
  const [position, setPosition] = useState("");
  const [wage, setWage] = useState(0);


  const addEmployee =()=>{
    console.log(name)
    Axios.post('http://localhost:6667/create', {name:name, age:age, country:country, position:position, wage:wage,})
    .then(()=>{
      console.log("Success from frontend")
    })

  }

  return (
    <div className="App">
      
  <label htmlFor="name">Name:</label>
  <input type="text" id="name" name="name" onChange={(event)=>{setName(event.target.value)}}></input>

  <label htmlFor="Age">Age</label>
  <input type="text" id="Age" name="Age"  onChange={(event)=>{setAge(event.target.value)}}></input>

  <label htmlFor="Country">Country</label>
  <input type="text" id="Country" name="Country" onChange={(event)=>{setCountry(event.target.value)}}></input>

  <label htmlFor="Position">Position</label>
  <input type="text" id="Position" name="Position" onChange={(event)=>{setPosition(event.target.value)}}></input>

  <label htmlFor="Wage">Wage</label>
  <input type="text" id="Wage" name="Wage" onChange={(event)=>{setWage(event.target.value)}}></input>
  
  <button onClick={addEmployee}>Submit</button>
    </div>
  );
}

export default App;

这是vele(index.js file)的代码:

const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors());
app.use(express.json());
const mysql = require('mysql2')
const db = mysql.createConnection({
    user: "root",
    host:"localhost",
    password:"password",   //it could be '' or 'password'
    database:'employeesystem'
});


app.post("/create", (request, response)=>{

    const name = request.body.name  
    const age = request.body.age
    const country = request.body.country
    const position = request.body.position
    const wage = request.body.wage

    db.query("INSERT INTO employee (name, age, country, position, wage) VALUES (?, ?, ?, ?, ?)", [name, age,country, position, wage], (err, result)=>{
        if (err){
            console.log(err)
        }else{res.send("Values Inserted!")}
    });
});

app.listen(6667, ()=>{
    console.log("Connection to server established")
})

因此,该表单正在使用Axios和Index.js(后端文件)将数据发送到后端,我正在使用Express来插入它进入我的数据库。我已经检查了任何数据库或表名称不匹配,并且没有。

一旦我使用按钮提交表单数据(触发包含Axios代码的函数),我会得到此错误:

”输入图像描述在这里”

我已经尝试更改端口,杀死它们,确保数据库查询和名称是一致的,甚至可以重新启动我的IDE,但无济于事。

我现在很累,对我很疲倦的一些帮助将不胜感激:/

所以,我快速搜索了谷歌搜索,发现某些端口是保留或不安全的。因此,我将端口更改为7889,现在有了这个错误。

I basically created a simple UI using React and from there, I am taking user input in a form and sending it to my backend using axios.
Here is the frontend code:

import {useState} from "react";
import Axios from "axios"
function App() {

  const [name, setName] = useState("");
  const [age, setAge] = useState(0);
  const [country, setCountry] = useState("");
  const [position, setPosition] = useState("");
  const [wage, setWage] = useState(0);


  const addEmployee =()=>{
    console.log(name)
    Axios.post('http://localhost:6667/create', {name:name, age:age, country:country, position:position, wage:wage,})
    .then(()=>{
      console.log("Success from frontend")
    })

  }

  return (
    <div className="App">
      
  <label htmlFor="name">Name:</label>
  <input type="text" id="name" name="name" onChange={(event)=>{setName(event.target.value)}}></input>

  <label htmlFor="Age">Age</label>
  <input type="text" id="Age" name="Age"  onChange={(event)=>{setAge(event.target.value)}}></input>

  <label htmlFor="Country">Country</label>
  <input type="text" id="Country" name="Country" onChange={(event)=>{setCountry(event.target.value)}}></input>

  <label htmlFor="Position">Position</label>
  <input type="text" id="Position" name="Position" onChange={(event)=>{setPosition(event.target.value)}}></input>

  <label htmlFor="Wage">Wage</label>
  <input type="text" id="Wage" name="Wage" onChange={(event)=>{setWage(event.target.value)}}></input>
  
  <button onClick={addEmployee}>Submit</button>
    </div>
  );
}

export default App;

Here is the code for the sever (index.js file):

const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors());
app.use(express.json());
const mysql = require('mysql2')
const db = mysql.createConnection({
    user: "root",
    host:"localhost",
    password:"password",   //it could be '' or 'password'
    database:'employeesystem'
});


app.post("/create", (request, response)=>{

    const name = request.body.name  
    const age = request.body.age
    const country = request.body.country
    const position = request.body.position
    const wage = request.body.wage

    db.query("INSERT INTO employee (name, age, country, position, wage) VALUES (?, ?, ?, ?, ?)", [name, age,country, position, wage], (err, result)=>{
        if (err){
            console.log(err)
        }else{res.send("Values Inserted!")}
    });
});

app.listen(6667, ()=>{
    console.log("Connection to server established")
})

So, the form is sending data to the backend using Axios and in the index.js (backend file), I am using express to insert it into my database. I have already checked any database or table name mismatch and there is none.

As soon as I submit my form data using a button (which triggers the function containing axios code), I get this error:

enter image description here

I already tried changing the ports, killing them, making sure database queries and names are consistent, and even restarting my IDE but to no avail.

Some help would be appreciated a lot cus I'm tired now:/

So ok, I did a quick googling and found that some ports are reserved or unsafe. So I changed the port to 7889 and now I got this error.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦妄 2025-02-20 20:02:43

在CreateConnection中指定端口,

配置您的应用程序在安全端口上运行,

例如,

  • f/e-端口-30xx

  • b/e-端口-50xx

  • d/b-端口-3306

      const db = mysql.CreateConnection({{
     主持人:“ localhost”,
     用户:“根”,
     密码: '',
     数据库:“员工系统”,
     港口:3036
     });
     db.connect();
     

在包装中。

{
 ..
 ..  
 "devDependencies": {
   ...
  },
"proxy": "http://localhost:5100"  //if server run on port 5100 or < 
}

Specify port in createConnection,

Configure your applications run on safe ports,

Example,

  • F/E - PORT - 30xx

  • B/E - PORT - 50xx

  • D/B - PORT - 3306

     const db = mysql.createConnection({
     host: 'localhost',
     user: 'root',
     password: '',
     database: 'employeesystem',
     port: 3036
     });
     db.connect();
    

In package.json of your react application add "proxy" It will allow app to pretend it is making request from the same port of server,

{
 ..
 ..  
 "devDependencies": {
   ...
  },
"proxy": "http://localhost:5100"  //if server run on port 5100 or < 
}
宫墨修音 2025-02-20 20:02:43

因此,实际上我是一个愚蠢的错误。在index.js文件中,我不是做result.send(),我在做res.send()

这是代码的更正块:

b.query("INSERT INTO employee (name, age, country, position, wage) VALUES (?, ?, ?, ?, ?)", [name, age,country, position, wage], (err, result)=>{
        if (err){
            console.log(err)
        }else{result.send("Values Inserted!")}
    });

So actually I was kind of making a silly mistake. In the index.js file, instead of doing result.send(), I was doing res.send()

Here's the corrected block of code:

b.query("INSERT INTO employee (name, age, country, position, wage) VALUES (?, ?, ?, ?, ?)", [name, age,country, position, wage], (err, result)=>{
        if (err){
            console.log(err)
        }else{result.send("Values Inserted!")}
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文