Axios给出404错误,而不是插入数据库
我基本上使用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,但无济于事。
我现在很累,对我很疲倦的一些帮助将不胜感激:/
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在CreateConnection中指定端口,
配置您的应用程序在安全端口上运行,
例如,
f/e-端口-30xx
b/e-端口-50xx
d/b-端口-3306
在包装中。
Specify port in createConnection,
Configure your applications run on safe ports,
Example,
F/E - PORT - 30xx
B/E - PORT - 50xx
D/B - PORT - 3306
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,因此,实际上我是一个愚蠢的错误。在index.js文件中,我不是做
result.send()
,我在做res.send()
这是代码的更正块:
So actually I was kind of making a silly mistake. In the index.js file, instead of doing
result.send()
, I was doingres.send()
Here's the corrected block of code: