get方法获取参数:
app.get('/api/user/login', function(req, res) {
console.log(req.query.username);
});
post方法获取参数:
app.post('/api/user/login', function(req, res) {
console.log(req.body.username);
});
使用axios请求api,我个人更倾向下边的写法:
post:
axios({
method: 'post',
url: ajaxUrl,
data: formData
}).then(res =>{
res = res.data;
}).catch(err =>{
});
get:
axios({
method: 'GET',
url: ajaxUrl,
params: data,
}).then(res => {
res = res.data;
}).catch(err => {
})

本文深入讲解了HTTP请求中GET和POST方法的区别与应用。GET方法适用于获取资源,参数附在URL之后;POST方法用于发送数据到服务器,常用于登录操作。文章通过示例代码展示了如何使用Node.js的Express框架处理这两种请求,并介绍了axios库的使用技巧。
833

被折叠的 条评论
为什么被折叠?



