MNK
MNK

Reputation: 131

update a json file data using angularjs

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.

Upvotes: 0

Views: 609

Answers (1)

Ali Uzair
Ali Uzair

Reputation: 36

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

Upvotes: 1

Related Questions