Front End
// js / jquery
var content = {
info : 'this is info',
extra : 'more info'
}
$.ajax({
type: 'POST',
url: '/tosave',
data: content
});
Node
// app.js
app.post('/tosave', function(req, res){
fs.writeFile('/saved.txt', function(err, data){})
});
This saves to the file as [object Object]
. I get the same result when I use JSON.stringify()
on content before sending it through the ajax request. However, if I stringify it in node.js it successfully saves as {"info":"this is the info","extra":"this is extra"}
.
My question is why this happens? Also, how would I go about converting it to a json string before the ajax request?