【发布时间】:2019-09-30 23:42:04
【问题描述】:
我正在尝试将 generalDetail 数据从我的反应前端传递到我的节点服务器。我目前收到连接被拒绝错误。
(选项http://localhost:5000/api/homenet::ERR_CONNECTION_REFUSED)。
这是我的 onSubmitForm 函数:
onSubmitForm(e){
e.preventDefault();
let data = {
generalDetail: this.state.generalDetails,
firstName: this.state.firstName,
middleName: this.state.middleName,
lastName: this.state.lastName
};
axios.post("http://localhost:5000/home", data).then(() => {
}).catch(() => {
console.log("Something went wrong. Plase try again later");
});
}
//end
onContentChange(fieldname, data){
console.log('On Content Change', data);
this.setState({
[fieldname]: data
});
}
}
这是我的 server.js 文件
const express = require('express');
const app = express();
// http://localhost:5000/api/home
app.post('/api/home', (req, res) => {
const data = [
{req.body.generalDetail},
];
res.json(data);
});
const port = 5000;
app.listen(port, () => `Server running on port ${port}`);
【问题讨论】:
-
您将 api 路由声明为“/api/home”,但将数据发送到“/home”。您需要确保将数据发送到正确的路线。
-
如果你改变你的 .catch 来处理错误,你会看到你得到一个 404。尝试使用: .catch( (error) => { console.log(error); ));
标签: javascript node.js reactjs express ecmascript-6