我需要提交一份表格。如果我以HTML形式进行操作,则目前可以使用,但是我确实需要做一些事情,因此需要做出反应。所以我在用axios。
但是,使用axios我没有得到回复。还有一个奇怪的地方,因为尽管我正在执行发布请求,但数据会在浏览器中显示为查询字符串...不确定这是否是正常行为。
这是我在服务器端的代码:
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。
我使用http-proxy-middleware将某些终结点代理到快递服务器。
After submitting the form I get this on the console:
我已经待了好几天了,在stackoverflow和其他地方徘徊,我完全被困住了……谁能给我一个提示我该怎么做?
答案 0 :(得分:0)
axios.create({
baseURL:const doesNotWork = 'https://boot.exmpledomain.com:8443/get/guy/1';
const doesWork = 'https://boot.exmpledomain.com:8443/get/guys';
const headers = {
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}
axios.get(doesNotWork, headers)
.then(res => {
console.log(res);
})
.catch(error => {
console.log("error", error);
const errorMsg = 'There was an error fetching the menu';
});
});
在发送请求之前设置baseURL。
答案 1 :(得分:0)
好吧,看来我已经找到了自己的答案...最后。谢谢大家的帮助!
问题很基本,很愚蠢。只是缺少“ event.preventDefault()”。就是……是的,我叫傻,在过去的几个小时里我一直在对自己说。