0

i have json data file, groups.json

 { 
       "groups" : [ 
       { "pname" : "group1", 
          "remarks" : "best" }, 
        { "pname" : "group2", 
          "remarks" : "not the best" } ,   
       { "pname" : "group3", 
         "remarks" : "affordable" } ] 
    }

And pushtest.html

    <body>
        <script type="text/javascript"> 
        var groups;      
        var len;
    $.getJSON('groups.json',function(data) {        
console.log(data.groups);   
        groups = data.groups;       
        len = groups.length;    
        });                 
        function save()     
        {       
        var user={};            
        user.pname=document.getElementById("name").value; 
        user.remarks=document.getElementById("remark").value;   
        console.log(user);          
        var l = JSON.stringify(groups);             
        var obj = JSON.parse(l);    
                    obj['groups'].push(user);   
        var v=JSON.stringify(obj); 
            console.log(v);         
        }       
         </script> 
        <form> 
        <input name="name" type="text" id="name" placeholder="name" /> 
        <input name="remark" type="text" id="remark" /> 
        <button onclick="save()" type="button">save data</button> </form> 
        </body>

Everything is getting fetched correctly from json file but its not getting appended to the json file. Plz help.

1 Answer 1

5

Manipulating data you have fetched from a URL doesn't change anything about the place you got the data from.

If it did, then every webpage in the world would be defaced within seconds.

If you want to update the JSON on the server then you need:

  1. To send the data back to the server (in a POST or PUT request)
  2. To have server-side code at the URL you send the data to that will read it and store it
Sign up to request clarification or add additional context in comments.

Comments

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.