kavita
kavita

Reputation: 1

Saving JSON to local disk

I have some json data in a variable using the .ajax method provided by jquery. During processing i am modifying some part of it. Now I wish to save it back so that a refresh will load the changed values. I am using the .ajax() with 'post' as type :

$.ajax({
type: 'POST',
url: "Scripts/"+fileName,
async: false,
data: JSON.stringify(myData),
dataType: "json",
success: function(data) {
alert('hi...');
},
error: function () {
alert('error...');
}});  

My json data has arrays etc :

{"workbook":{"excelname":"D:\/JavaProject\/SpreadsheetScanningFromRepository\/testing\/Test Data - Relationship\/multilevelLabels.xls","sheet":{"name":"Sheet1","structures":{"structure":{"StructureComment":null,"orientation":"Labels are in Rows","CompleteData":[{"label":"Company name","data":[["Bathni",{"datatype":"String Data"}],["Bathni",{"datatype":"String Data"}],["Bathni",{"datatype":"String Data"}],["Bathni",{"datatype":"String Data"}],["BBM",{"datatype":"String Data"}],["BBM",{"datatype":"String Data"}],["BBM",{"datatype":"String Data"}],["Finsys",{"datatype":"String Data"}],["Finsys",{"datatype":"String Data"}],["Avaya",{"datatype":"String Data"}]]}...

The call shown above returns a success and prints hi but no change in the file!! async is false so it should have made the change immediately. The url given is the same one i use to read json data from the local filesystem so that should not have any issues.

Please give me any hints/ suggestions for making this work!

Upvotes: 0

Views: 1820

Answers (2)

Nihar Sawant
Nihar Sawant

Reputation: 534

It depends where you want to store the data. There are two ways to dealing with it

  1. Server Side Storage: It depends on which server you are using and do proper file handling with all the methods provided in it in order to save it.
  2. Client Storage: It is introduced in HTML5 which is supported by all latest versions of modren browsers. All you have to do is-

    To Save in localstorage - Assume var bar = 'abc' is declared. localstorage.setItem('foo', bar); where foo acts as key and bar acts as a its value.

    To Load from LocalStorage - var value = localstorage.getItem('foo'); This will assign 'abc' to value.

If you are using Google Chrome then you can view the localstorage in Developer's Tool->Resources->Local Storage

Upvotes: 1

aorcsik
aorcsik

Reputation: 15552

Edit: In the case of local disk. Without a file system layer (local apache webserver for example, or other protocol on the local system) this cannot be done from the browser.

To change files on the server, your posted data should be handled by a server side script that is responsible for file operations on the server. Just posting data to a file would only return your file, since nothing in it handles the posted values.

You should pass your data to a post handler, not the file you want to modify.

This post handler should do something like this:

  • accept posted data (automatic in most cases)
  • open your file for writing
  • write you posted data into the file (in json encoded format)
  • close the file
  • print the contents of the modified file

You can use all sorts of languages for this, like: PHP, Ruby or Perl

Upvotes: 0

Related Questions