smus 1 Posted May 30, 2018 Report 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 Quote Link to post Share on other sites
Ingolme 1,019 Posted May 30, 2018 Report 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. Quote Link to post Share on other sites
smus 1 Posted May 31, 2018 Author Report 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 Quote Link to post Share on other sites
Ingolme 1,019 Posted May 31, 2018 Report 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 Quote Link to post Share on other sites
smus 1 Posted May 31, 2018 Author Report Share Posted May 31, 2018 Thanks, Ingolme Quote Link to post Share on other sites
smus 1 Posted July 27, 2018 Author Report 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" Quote Link to post Share on other sites
Ingolme 1,019 Posted July 27, 2018 Report 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 Quote Link to post Share on other sites
smus 1 Posted July 28, 2018 Author Report 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.