0

I have one json file. I am getting json file data through http.get() method and storing into one scope variable. Now I am modifying the json file data as I want. How to pass the parameters to through http.post() and update json file data.

    $http.get('/HCConfig/ValidityReminderSettings.json').then(function (response,data)                     $scope.value=console.log(response.data.ValidityReminderSettings);
$scope.reminder1 = response.data.ValidityReminderSettings.Reminder1;
$scope.reminder2 = response.data.ValidityReminderSettings.Reminder2;
    });

Json file looks like:

"ValidityReminderSettings": 
{ 
   "Reminder1" : "30",
   "Reminder2" : "15"      
}    

I am passing parameters that I need to update into json file

$scope.UpdateValidityReminderSettings =function()
{
    var newremainderVal1=document.getElementById('Reminder1').value; 
    var newremainderVal2=document.getElementById('Reminder2').value;

    $scope.ArrayValue=[{"Reminder1":newremainderVal1},
                         {"Reminder2":newremainderVal2}];
    var  datas= $scope.ArrayValue;
    $http.put('/updateJsonFile' + datas).then(function (response) 
   {
          $scope.ServerResponse = data;
   });

} In server page how should I pass this and update that json file

var jsonfileConfig = require('./public/HCConfig/ValidityReminderSettings.json');

app.put('/updateJsonFile/:data',jsonfileConfig);

I am getting error. I am doing something wrong while passing this parameters in server page. How to pass and update this json file

Thanks in advance.

1 Answer 1

1

Remove + (plus) and use , (comma) in post request

Example:

$http.post('/updateJsonFile' , {"data":datas}).then

It looks like you are using nodejs on server side..

app.post('/updateJsonFile/', function(req, res){
    console.log(req.body.data);
    fs.writeFile('./public/HCConfig/ValidityReminderSettings.jso‌​n', req.body.data);
});

You can use fs package to write json to file

Sign up to request clarification or add additional context in comments.

8 Comments

app.post('/updateJsonFile/', data); Referrence error in server page data is not defined
i am not writing new json file. I am updating the existing json file values. Now I have used $htttp.put() even though i am getting error
I have updated code... previous code was just for giving idea.. now I have improved code on angular and nodejs... on server you will get json in req.body.data.
in console.log(req.body.data) I am getting whatever I changed in reminder1 and reminder2 but it is not updating in that file . fs.writeFile('./public/HCConfig/ValidityReminderSettings.jso‌​n', req.body.data); I think this line is not working.
Use following code and inform me what you get on console fs.writeFile('./public/HCConfig/ValidityReminderSettings.jso‌​‌​n', req.body.data , 'utf8', function(err,data) { if (err){ console.log(err); }; res.end(data); });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.