V
V
Vitaly2015-06-22 11:45:55
JavaScript
Vitaly, 2015-06-22 11:45:55

How to specify correct path to local json file in angularjs on post request?

Hello.
I write phonegap applications in angulajs.
The task is to check the data from the local json file, and, if necessary, add new data to this file.
Reading a file with a get request

$http.get('/pub/data.json') .then(function(res){
                  $scope.gotdata = res.data;})

Everything works fine, but when I need to add something, I use a post request in the form:
$http.post('/pub/data.json' ,newDataToAppend) .then(function(res){
                  console.log('done');})

I get an error 404, file not found :(
It turns out that the file is visible with a get request, but not with a post :(
Please tell me what I'm doing wrong

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Melnikov, 2015-06-22
@mlnkv

Do you even imagine what a post request is and reading and writing to local files ?!)
docs.phonegap.com/en/edge/cordova_file_file.md.html
file reading:

// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
function onDeviceReady() {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
  fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
  fileEntry.file(gotFile, fail);
}

function gotFile(file){
  readDataUrl(file);
  readAsText(file);
}

function readDataUrl(file) {
  var reader = new FileReader();
  reader.onloadend = function(evt) {
    console.log("Read as data URL");
    console.log(evt.target.result);
  };
  reader.readAsDataURL(file);
}

function readAsText(file) {
  var reader = new FileReader();
  reader.onloadend = function(evt) {
    console.log("Read as text");
    console.log(evt.target.result);
  };
  reader.readAsText(file);
}

function fail(error) {
  console.log(error.code);
}

writing to file
// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
function onDeviceReady() {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
  fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
  fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
  writer.onwriteend = function(evt) {
    console.log("contents of file now 'some sample text'");
    writer.truncate(11);
    writer.onwriteend = function(evt) {
      console.log("contents of file now 'some sample'");
      writer.seek(4);
      writer.write(" different text");
      writer.onwriteend = function(evt){
        console.log("contents of file now 'some different text'");
      }
    };
  };
  writer.write("some sample text");
}

function fail(error) {
  console.log(error.code);
}

V
Vitaliy, 2015-06-22
@Scorpiored88

I need to update data in this local json file.
how to do it right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question