0

The problem that I have is, that I want to store data that I got in JavaScript into a JSON file.

For example the JSON file at the beginning looks like this:

{
    "ip": "none",
    "user": "none"
}

Now I have defined to JavaScript variables:

const ip = '127.0.0.1';
const user = 'me225hey';

I've already tried the following:

$.getJSON( "path/to/foo.json", function( data ) {
      data.ip = ip;
      data.user = user;
}); 

This way I could only retrieve the data but not store it. After searching in the internet I could only find a way to retrieve the data.

If it isn't possible to store data in a JSON file by JavaScript code. Where should I store my data otherwise? I can't use SQL for that. It should be locally and possible in Angular.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Keimeno
  • 2,512
  • 1
  • 13
  • 34
  • 4
    JavaScript can not write to a file from the browser. You need something on the server that can write to a file. – epascarello Apr 24 '19 at 16:58
  • In angular, you have to create a service for that. – Milad Bonakdar Apr 24 '19 at 17:01
  • You can still store in cookie, local or session storage if you have to. – choz Apr 24 '19 at 17:01
  • You can save in DATA, please see this link https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes – Tony Dong Apr 24 '19 at 17:02
  • Just to add my 2c, there a variety of offline storage mechanisms, namely: localStorage, sessionStorage, indexedDB, cookies, Cache API. Have a look at this presentation for more info: https://docs.google.com/presentation/d/11CJnf77N45qPFAhASwnfRNeEMJfR-E_x05v1Z6Rh5HA – Craig Wayne Apr 24 '19 at 17:06

2 Answers2

0

To store data you have several options,

  1. Use a Service since you are using Angular
  2. Store it on Local Storage / Session Storage etc... check image below

enter image description here

  1. Or you can use Cookies
Ilir Hushi
  • 99
  • 5
0

You can't store it locally as a file, not with js at least. Either store it on a server, or check out local storage, or session storage to store it locally. But it wouldn't be stored as a file...and if someone clears their cache, it would be gone. So this would be only a temporary storage.

Look here

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37