【问题标题】:Access AJAX data on node.js server访问 node.js 服务器上的 AJAX 数据
【发布时间】:2016-02-03 01:39:53
【问题描述】:

我正在使用 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 {}
});

但是,当我尝试访问数据时,我总是得到一个空对象。

有人可以帮忙吗?

提前致谢!

【问题讨论】:

  • 您是否尝试过直接在 url 中发送而不是 data 属性?

标签: ajax node.js api server


【解决方案1】:

我知道了 - 这是console.log(req.query)

【讨论】:

    【解决方案2】:

    使用帖子代替获取:

        $.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()。

    【讨论】:

    • 嗨 - 我实际上是在尝试控制台记录服务器端的数据,而不是客户端。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多