1

I want to update the existing json file in AngularJS

JSON file:

 {  
    "text1": "Click here to edit!",
    "text2": "Click here to edit!",
    "text3": "Click here to edit!",
    "text4": "Click here to edit!"  
}

I want to update this JSON file as:

text1: "Abc"

and save this changes in JSON file

7
  • 1
    Is the JSON file in the server? Commented Nov 17, 2015 at 11:02
  • @Gaurav Quick question Are you trying to send updated/modified json to server? Commented Nov 17, 2015 at 11:39
  • @Gaurav Can you clearly mention your problem. Commented Nov 17, 2015 at 11:40
  • @MohanSingh Yes I want to send updated/modified json to server Commented Nov 17, 2015 at 12:21
  • @Gaurav Sorry for delay, are you still facing the same issue? Commented Nov 18, 2015 at 7:50

2 Answers 2

3

You can not update a json file without using a server-side language like PHP or python. Basically it is security compliance. For more understanding kindly go through https://docs.angularjs.org/guide/security https://docs.angularjs.org/api/ng/directive/ngCsp and https://developer.mozilla.org/en-US/docs/Web/Security/CSP

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

Comments

1

Imagine you have getjson.php and savejson.php in the server which work exactly as their names suggest.

Now use $http service of Angular to retrieve your json from the server.

$http.get("getjson.php").then(function(response){

     $scope.myJsonObject = response.data;

     //Your json becomes JS object here. Change it the way you want
     $scope.myJsonObject.text1 = "Abc"; 

});

Use $http service again to send your json back to the server.

$http({
           method: "post",
           url: "savejson.php",
           data: $scope.myJsonObject,
           headers: { 'Content-Type': 'application/json; charset=utf-8' }
});

This is the basic. Please note that you need to do your php part to save/load your json file. Also you should handle errors of the $http service.

Please see how $http service and promises work.

1 Comment

It will be the same json file as long as getjson.php and savejson.php deal with the same file.

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.