Change file contents with ajax

Asked

Viewed 154 times

-1

Hello, first, I would like to clarify that I searched both here in the stack, as in other sites something that answered my question, but I did not find. My question is, how can I rewrite the contents of a server JSON file using AJAX? In this case, I can take its content and use it in the browser normally, however, when changing something in the browser, I would like to save/overwrite the changes in the original file.

Current Code:

JSON

[{
    "nome":"Borin Gnur",
    "profissao":"Bibliotecário",
    "imagem":"img/borin.png"
}, (...)]

JS

var personagensJSON;
function recuperaPersonagens(){
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function(){
        if(this.readyState == 4 && this.status == 200){
            return personagensJSON = JSON.parse(this.responseText);  
        }
    };
    xhttp.open("GET", "personagens.json", true);
    xhttp.send();
    //selecaoPersonagem();
}
  • You will need a server-side language analyzing the request and writing to the file. With PHP, you could use file_put_contents, for example.

  • Dude, I already explained to you in chat that this is impossible.

  • I didn’t say I don’t want to use PHP, I said for now I’m not using, precisely why I posted the question, to figure out a way to do this '-'

  • @LINQ, so I posted the question, to see what options I have with other languages '-'

  • @Andersoncarloswoss Perl :p

  • PERL and PHP as far as I know

  • any of the languages, in the question, I just left informed that I did not use PHP at any time

  • I removed from there not to cause more confusion aheuahe

  • Now another detail: are you already making a POST request with the new data to the server or have difficulty in this part too? If yes, you can ask the question?

  • just like it is in the question, I want to know how to accomplish it, change the content and such, I’m not making any further request

Show 5 more comments

1 answer

1


Your problem is:

change something in the browser, I would like to save/overwrite the changes in the original file.

What you have to do is make the changes in the browser, and then send (possibly via AJAX) the changed file to the server for the server to save it (probably using one of the verbs HTTP POST, PUT or PATCH).

The server must provide a service for this purpose, in addition to dealing with appropriate authentication and authorization issues (to prevent malicious users from messing with the server files).

It is not possible to save directly via the server. The reason is that the file is modified in the browser of a particular computer (or mobile phone or similar thing) X and it must be saved in the computer file system Y. There is no way to do the data that are in X appear in Y without them being sent in any way from the device X up to the device Y, and the best way to send them is precisely through HTTP, which is the protocol already used by the browser that is running on X on a website made available via HTTP by the server Y.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.