admin管理员组文章数量:1421000
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!
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!
Share Improve this question asked Jul 2, 2013 at 15:28 dopplesoldnerdopplesoldner 9,53912 gold badges46 silver badges57 bronze badges 1-
You can create that object by simply writing
var json = { Id: 100, plugins: [{ Name: 'Test', Version: '2' }] }
– SLaks Commented Jul 2, 2013 at 15:39
2 Answers
Reset to default 6As long as you have the bodyParser()
middleware installed, Express should parse the JSON request payload into req.body
.
Your creation is wrong. JSON is a key-value based storage, meaning it has the syntax of a hash. Try this:
var json = {};
json['Id'] = 100;
var plugins = [];
plugins.push({"Name" : "Test", "Version" : "2"});
json['plugins'] = plugins;
本文标签: javascriptHow to pass JSON object to nodejs serverStack Overflow
版权声明:本文标题:javascript - How to pass JSON object to node.js server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745337352a2654101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论