1

I am trying to add data to *.JSON file, that has only this text: {}. Ajax returns "success", but the file remains as before. Also I have noticed if the JSON file is empty, nothing happens at all. Please, help me to find a solution. I don't get where the trouble is. Maybe, you know what else can be the reason of the problem. Thank you.

The code of the ajax:

 $.ajax({
     type: "POST",
     url: "data/test.json",
     data:  JSON.stringify({ "userName": "1", "password" : "1" }),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(data){alert("success");},
     fail: function(errMsg) {alert("fail");
     }
});
anna_manzhula
  • 326
  • 2
  • 10

3 Answers3

1

The best solution was to install denver and use PHP code on server-side.

This is code in the js file.

  deleteNewsItem: function(newsId, callback, scope) {
     var dataPath = this.dataPath;
      $.getJSON(dataPath + '/delete-news.php?id='+newsId, function(rawHeadlines) { 
         var headlines = [];
         $.each(rawHeadlines, function(index, rawHeadline) {
            headlines.push(rawHeadline.id);
        } 
     });
             if (callback) {
             callback.call(scope, headlines);
     }
     };

This is the code in the "delete_news.php" file.

<?php

$deleteId = isset($_GET['id'])?$_GET['id']:false;
   if ($deleteId){
     $content = file_get_contents('delete-news.json');
     $json = json_decode($content);

$newData = array();
foreach($json as $key=>$id){
$newData []= (array)$id;
}
$newData []= array("id"=>(int)$deleteId);


$fill_view = json_encode($newData);
file_put_contents('delete-news.json', ($fill_view));
echo $fill_view;
} else {
echo json_encode(array());
}
exit();

?>
anna_manzhula
  • 326
  • 2
  • 10
0

It is not possible to save a json file with jquery alone. You must implement a server side code in order to make that work.

Dima Grossman
  • 2,800
  • 2
  • 21
  • 26
  • Thank you for the answer. But if the task contains no server-side code, what should i do to save data into the file? Or how can I find this code... I am sorry, I m very new to JS. – anna_manzhula Mar 18 '15 at 16:55
-1

You have some error in your code, first of all there is no such thing as failure, but error or fail . Try this and tell me if it helps. I think there is no way to save data to the file using javascript/jquery (for security reasons) if you want to get or store information you need server side, if you are only trying to store small amount of data you can use cookies or localStorage("Key", "Value As Json");

You can save data into localStorage like this:

    $.get("data/test.json", null, function (data) {

    localStorage.setItem("data", JSON.stringify(data));
});

but unfortunately as i said earlier you can't save to the file using javascript/jquery, try to using localStorage for temporary storage.

Konrud
  • 1,114
  • 9
  • 19
  • Thank You for the answer. Nothing has changed. The result is the same. – anna_manzhula Mar 18 '15 at 16:56
  • I think there is no way to save data to the file using javascript/jquery (for security reasons) if you want to get or store information you need server side, if you are only trying to store small amount of data you can use cookies or localStorage("Key", "Value As Json"); – Konrud Mar 18 '15 at 17:05
  • Yes, I am trying to save the numbers of deleted Items' Ids of the ItemList. The structure of the project assumes that I should store this data into JSON file.. or maybe I can get this file into the local storage? – anna_manzhula Mar 18 '15 at 17:12
  • localStorage saves data in string representation, i.e. you can save json in it but not the file. – Konrud Mar 18 '15 at 17:15