Axios帖子未收到答案(ReactJS) [英] Axios post doesn't receive answer (ReactJS)
问题描述
我需要提交一份表格.如果我以HTML形式进行操作,则目前可以使用,但是我确实需要做一些事情,因此需要做出反应.所以我在用axios.
I need to submit a form. It currently works if I do it in the HTML form, but I really need to make some stuff after so I need to do it in react. So I'm using axios.
但是,使用axios我没有得到回复.还有一些奇怪的事情,因为尽管我正在执行发布请求,但数据会在浏览器中显示为查询字符串...不确定这是否是正常行为.
However, using axios I don't get a response back. Also there's something strange, because although I'm doing a post request, the data appears as a query string on the browser... Not sure if that's the normal behaviour.
这是我在服务器端的代码:
Here's my code, on the server side:
app.post("/auth/register", async function(req, res, next) {
// some code
// my responses are like res.json("/signup/success");
}
在客户端:
onSubmit(event) {
axios({
method: "post",
url: "/auth/register",
data: {
username: this.state.username,
password: this.state.password,
accountName: this.state.accountName
},
withCredentials: true
}).then(result => {
console.log(result);
});
}
我正在使用express在端口5000上运行服务器,并使用create-react-app在端口3000上运行服务器.要管理身份验证,请使用password.js.
I'm running a server on port 5000 using express, and I used create-react-app to run a server on port 3000. To manage authentication, passport.js.
我使用http-proxy-middleware将某些终结点代理到快递服务器.
I use http-proxy-middleware to proxy some endpoints to the express server.
我已经待了好几天了,在stackoverflow和其他地方徘徊,我完全被困住了……谁能给我一个提示,我该怎么办?
I've been at this for days, wandering around stackoverflow and everywhere else, and I'm completely stuck... Can anyone please give me a hint on what can I do?
推荐答案
好,看来我已经找到了自己的答案...最后.谢谢大家的帮助!
Ok so it seems I've found my own answer... Finally. Thank you all for your help!
问题很基本,很愚蠢.只是缺少"event.preventDefault()".就是这样...是的,我叫傻,在过去的几个小时里我一直在对自己说.
The problem was pretty basic, pretty dumb. Simply the lack of "event.preventDefault()". Just that... Yeah call me stupid, I've been telling that to myself for the past hours
这篇关于Axios帖子未收到答案(ReactJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!