Skip to content Skip to sidebar Skip to footer

Trying To Parse Json Object From Post Request In Express V4 Using Body-parser

I'm currently teaching myself more about server code, specifically using Node.js and Express, and I'm having a lot of trouble with receiving and parsing a JSON object sent from a P

Solution 1:

"[object Object]" is the default result of JavaScript's implicit toString operation, which it uses when trying to write a string representation of that object to the file.

Try writing JSON.stringify(req.data) to the file instead.

Also, on the client side – consider changing your Content-Type header to match:

xhr.setRequestHeader("Content-Type", "application/json");

Solution 2:

If your post body is expected to be JSON, then change this line

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

To

 xhr.setRequestHeader("Content-Type", "application/json");

Post a Comment for "Trying To Parse Json Object From Post Request In Express V4 Using Body-parser"