I have a Polymer core-ajax component that sends some data to a Node.js server. The data is sent correctly (I can parse it using the Go web server), but Node parses it as a string body, which is the key to the empty string in the JSON object:
{ '{"count":-1,"uid":1}': '' }
This is the code that sends the request from Polymer:
sendCount: function(change) { console.log("Sending..."); console.log(JSON.stringify({"count": change, "uid": this.uid})); // ^ This prints: {"count":-1,"uid":1} this.$.ajax.body = JSON.stringify({"count": change, "uid": this.uid}); this.$.ajax.go(); }
This is the Node code:
app.post("/post", function(req, res) { console.log(res.headers); console.log(req.body); // Prints { '{"count":-1,"uid":1}': '' } res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(req.body)); });
When I get the response back, it returns invalid JSON.
How should I parse JSON correctly in Node?
also:
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false }));
source share