2

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.

3
  • You can use ajax to request the file from the server and then in the success callback you can process the file contents Commented 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. Commented Oct 14, 2013 at 9:29
  • i am using pure client side languages only so database would never come into picture. Commented Apr 18, 2014 at 11:23

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

ya i thought of it but since i was not allowed to use PHP i tried using SHELL instead and it worked thanks..
2

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

Comments

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

// your logic;

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.