例如:
axios.post('/api/user', { name: 'John' })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
例如:
axios.get('/api/user', { name: 'John' })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
例如:
router.post('/api/user', (req, res) => {
const user = new User({ name: req.body.name });
user.save((err) => {
if (err) {
console.log(err);
return res.json({success: false, msg: 'Failed to save user'});
}
return res.json({success: true, msg: 'User saved'});
});
});
在这段代码中,我们检查了API实现中的错误。 如果存在错误,我们返回一个带有错误信息的JSON响应,否则返回成功的JSON响应。
通过调试Axios实例和服务器端API代码,您应该能够解决Axios出现404错误的问题。