我正在托管在域(server.domainname.com)上的 NodeJS 服务器上运行一些 api 测试,同时从我的本地开发环境(即 localhost:3000)发布请求。
我面临的问题是在服务器的 axios 帖子 上,我得到:
404 Not Found
而所有资源和路径都在服务器文件中配置。
这是一个片段:
const mailData = {
fname: 'FirstName',
lname: 'LastName',
mail: 'email@example.com',
amountPaid: 25
}
axios.post("https://server.domainname.com/topup", mailData).then(() => {
console.log('post success')
}).catch((error) => {
console.log(error);
})
这是在我的服务器文件中接收此路径的 api 配置:
app.post('/topup', (req, res, next) => {
let mailDetails = req.body;
sendEmail(mailDetails, "topUp");
next();
})
不幸的是,它在控制台和浏览器开发者工具中记录了 Error: Request failed with status code 404
:POST https://server.domain.com/topup 404 Not Found
答案 0 :(得分:0)
可能
<块引用>https://server.domainname.com/topup
应该切换到您正在使用的域或 你可以试试 /topup
const mailData = {
fname: 'FirstName',
lname: 'LastName',
mail: 'email@example.com',
amountPaid: 25
}
axios.post("/topup", mailData).then(() => {
console.log('post success')
}).catch((error) => {
console.log(error);
})
答案 1 :(得分:0)
试试res.end()
app.post('/topup', (req, res, next) => {
let mailDetails = req.body;
sendEmail(mailDetails, "topUp");
res.end() // add this!
next();
})