如何使用ExpressJS从jQuery Ajax发布中获取数据

rnldpbln

嗨,所以我有一个jquery发布数据,我正在发送:

                 $.ajax({
                    type: "POST",
                    url: app.config.backend_2 + '/notifications/usernames',
                    data: data,
                    contentType: 'application/javascript',
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                    }
                });

这是我的快递收货人:

 exports.save_usernames_for_notifications = function (req, res, next) {
    var start = function () {
       console.log(req);
    };

     start();

};

我如何从ajax获取数据并将其记录在save_username_for_notifications函数中?

提姆

您需要在expressjs应用程序中使用主体解析器中间件来解析JSON req对象。

https://www.npmjs.com/package/body-parser

要配置它,您需要此代码

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // this is used for parsing the JSON object from POST

然后要在快速应用程序中获取数据,只需执行此操作

console.log(req.body.data)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章