How is read/write a text file possible in javascript if my text file is stored in server?

StackOverflow https://stackoverflow.com/questions/19357112

  •  30-06-2022
  •  | 
  •  

Question

i am trying to read a file from the server and then write back to that same file again using javascript. Is it possible to read/write the text file using JSON,Javascript or Ajax.

Was it helpful?

Solution 2

JavaScript is client-side language, so it don't have access to server. To do it you have to use AJAX and some server-side language, eg. PHP to handle requests.

OTHER TIPS

It is not possible to edit server sided files using pure Javascript, imagine the security issues :)

What you could do is to use a server sided language, like PHP or ASP, and send an ajax request to the server containing the information you want to write.

You can read a file with javascript via an AJAX call. You cannot save a file with javaScript. You will need a server side language like PHP.

You can use: step 1) jquery to load a file: http://api.jquery.com/load/ step 2) jquery to send the modified text to the .php : script http://api.jquery.com/jQuery.post/ step 2.1) save the data in php : http://php.net/manual/en/function.file-put-contents.php

$.getJson("/main.txt",function(jsonObject){

// your logic;

})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top