如何将JSON对象传递给node.js服务器(How to pass JSON object to node.js server)

我有一个测试JSON对象如下

{ Id: 100, plugins: [{ Name: 'Test', Version: '2' }] }

我用它创建的

var json = {}; json.Id = 100; var plugins = []; json.plugins = plugins; json.plugins.push({"Name" : "Test", "Version" : "2"});

我有一个客户端函数发出一个AJAX发布请求,如下所示

function postJSON() { $.ajax({ type: 'POST', url: 'features', data: json, dataType: 'json' }); }

我现在只想输入console.log(req)来尝试在我的服务器上读取这些信息,但它似乎没有得到我的json对象。

app.post("/features", function(req, res) { console.log(req); })

感谢帮助!

I have a test JSON object as follows

{ Id: 100, plugins: [{ Name: 'Test', Version: '2' }] }

which I created using

var json = {}; json.Id = 100; var plugins = []; json.plugins = plugins; json.plugins.push({"Name" : "Test", "Version" : "2"});

I have a client side function making an AJAX post request as follows

function postJSON() { $.ajax({ type: 'POST', url: 'features', data: json, dataType: 'json' }); }

I am trying to read this information on my server by just typing console.log(req) for now but it does't seem to be getting my json object.

app.post("/features", function(req, res) { console.log(req); })

Thanks for help!

最满意答案

只要安装了bodyParser()中间件,Express就应该将JSON请求有效负载解析为req.body 。

As long as you have the bodyParser() middleware installed, Express should parse the JSON request payload into req.body.

更多推荐

json,req,function,console,电脑培训,计算机培训,IT培训"/> <meta name="des