smus Posted May 30, 2018 Share Posted May 30, 2018 (edited) I have 3 files: html that is displaying data from json file using AngularJS, json file containing the data, script.js that stores the scripts. HTML file also has a form, from which I want to add data to my json file (onsubmit="insertData()"). insertData function is going to be in script.js I know, I may use .push method for this, but I don't know how to apply to the json file in this case. script.js var prg = angular.module('ajsprogram', []); prg.controller("outercont", function($scope, $http) { //function to display json file information $http.get('airlines.json').success(function(data){ $scope.info = data; $scope.airlineorder = 'airline'; }); }); function newairline(an,hb,ur,cs){ //new function to insert a data } airlines.json [ { "airline" : "British Airways", "hub" : "LHR", "url" : "http://britishairways.com", "callsign":"SPEEDBIRD" }, { "airline" : "Air France", "hub" : "CDG", "url" : "http://airfrance.com", "callsign":"AIRFRANS" }, { "airline" : "Virgin Atlantic", "hub" : "LGW", "url" : "http://virginatlantic.com", "callsign":"VIRGIN" }, { "airline" : "RyanAir", "hub" : "DUB", "url" : "http://ryanair.com", "callsign":"RYANAIR" }, { "airline" : "Emirates", "hub" : "DXB", "url" : "http://emirates.com", "callsign":"EMIRATES" } ] Edited May 30, 2018 by smus Link to comment Share on other sites More sharing options...
Ingolme Posted May 30, 2018 Share Posted May 30, 2018 If you want to write to a file on the server then you will need a server-side program that does the writing. Javascript is not able to write to the server. Link to comment Share on other sites More sharing options...
smus Posted May 31, 2018 Author Share Posted May 31, 2018 10 hours ago, Ingolme said: If you want to write to a file on the server then you will need a server-side program that does the writing. Javascript is not able to write to the server. I know, but I have asked the same question on StackOverflow. Can you please consider their solution and advise me if there is a way to somehow use it in separate files? Here is the link: https://stackoverflow.com/questions/50590055/angularjs-inserting-data-to-a-json-file/50596513#50596513 Link to comment Share on other sites More sharing options...
Ingolme Posted May 31, 2018 Share Posted May 31, 2018 The answer given on StackOverflow will update the data structure, but it will not be saved to a file. The data disappears as soon as the user leaves the page. There is no way for Javascript to write to a file. You have to use a server-side program to write to the file for you. Javascript can send the data to the server, but the server has to write the data to the file. 1 Link to comment Share on other sites More sharing options...
smus Posted May 31, 2018 Author Share Posted May 31, 2018 Thanks, Ingolme Link to comment Share on other sites More sharing options...
smus Posted July 27, 2018 Author Share Posted July 27, 2018 May I refer to this topic, if an employer asks me to manipulate data from json file using javascript? Because it is already the second time they want me to do "something impossible" Link to comment Share on other sites More sharing options...
Ingolme Posted July 27, 2018 Share Posted July 27, 2018 This is a public forum, you can show it to anybody. The task is impossible using only Javascript, but do you have access to a server-side programming language? 1 Link to comment Share on other sites More sharing options...
smus Posted July 28, 2018 Author Share Posted July 28, 2018 No, those were just interview tasks with strict condition to use JS or one of its frameworks. But I decided to show that data from a form can, indeed, be added using push() method for arrays, if the json structure is inside html file. It works well, however, of course, the json data is not changing physically. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now