I want to get the content of a txt file in JavaScript, so I want to call a php file to open and save the content of the text file into a variable and return it to JS is that possible?
I've sent variables from js to php with $.post() and I tried to use $.get("php file") but I can't return the file content can anybody help me?
            Asked
            
        
        
            Active
            
        
            Viewed 625 times
        
    1
            
            
         
    
    
        Rembrandt Dosal
        
- 11
- 3
- 
                    Do you want to read a file (php) from the server and use its contents inside javascript? – Jan May 21 '19 at 22:48
- 
                    If you want to read from a text file and then return it to javascript, then why don't you simply read the text file from javascript? One way is you can use [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). – Mystical May 21 '19 at 22:55
1 Answers
0
            
            
        To read/get a file from the server and use it contents in javascript/jquery, you can use the shorthand-method $.get. Inside the callback-function, you are able to process the data returned from the server.
$.get("file.php", function(data) {
  console.log(data);
});
 
    
    
        Jan
        
- 2,853
- 2
- 21
- 26