我正在使用node.js构建应用程序,并希望了解如何在服务器端访问我的AJAX GET请求数据。例如:
客户端AJAX
$.ajax({
url: "http://localhost:3000/api/test",
type: "GET",
data: {email: 'myemail@gmail.com', name: 'george'}, //send this to server
success: function() {
},
error: function() {
}
});
服务器端API
router.get('/api/test', function(req, res) {
console.log(req.body) //should print data from AJAX request; currently returns empty {}
});
但是,当我尝试访问数据时,我一直得到一个空对象。
有人可以帮忙吗?
提前致谢!
答案 0 :(得分:0)
使用post而不是get:
$.ajax({
url: "http://localhost:3000/api/test",
type: "POST",
data: {email: 'myemail@gmail.com', name: 'george'}, //send this to server
success: function() {
},
error: function() {
}
});
router.post()
。
答案 1 :(得分:0)
我明白了 - 它是console.log(req.query)