Answer the question
In order to leave comments, you need to log in
How to write an object received via AJAX to an already existing JSON?
Good afternoon everyone! Question about interoperability between Node.js, Express and JSON.
The story is this: I have several forms whose values I want to store in JSON. That is, I enter anything into the form, press the save button, and they fly to the server in the form of such an object:
{
"name": "345678",
"mail": "98765678233123123",
"phone": "900213123",
"date": "-----",
"summ": "-----",
"visits": "-----",
"active": "-----"
}
$('.modal__window__right__save div').click(function(){
$.ajax({
url: "http://localhost:3000",
type: "POST",
data: userStr,
success: function() {
console.log('data was sent');
},
dataType: 'JSON'
});
app.post('/', function (req, res, next) {
req.addListener('data', function(postDataChunk) {
// Может быть ошибка где то тут?
let postData = JSON.parse(postDataChunk);
console.log('Новые данные получены');
// Или где то тут?
jsonfile.writeFileSync('build/data/user.json', postData, {spaces: 2}, function(err){
console.log(err);
});
});
next();
});
{
"users": [
// here
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question