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.
-
You can use ajax to request the file from the server and then in the success callback you can process the file contentsjasonscript– jasonscript2013-10-14 09:27:31 +00:00Commented Oct 14, 2013 at 9:27
-
Why write to a text file and not a database? Think about what will happen when multiple people are running the code, you'll end up locking the file.Jeremy Thompson– Jeremy Thompson2013-10-14 09:29:37 +00:00Commented Oct 14, 2013 at 9:29
-
i am using pure client side languages only so database would never come into picture.Abhi– Abhi2014-04-18 11:23:59 +00:00Commented Apr 18, 2014 at 11:23
Add a comment
|
4 Answers
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.
1 Comment
Abhi
ya i thought of it but since i was not allowed to use PHP i tried using SHELL instead and it worked thanks..
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