Reading from text file on server using jquery

I have a text file (.txt) on the server. It contains only one word, i.e. ON or OFF. How can I use jQuery or Pure Javascript to read this word from a file.

I think along the lines of $.ajax.

+6
source share
3 answers

You can use the $.get method to send an AJAX GET request to a resource located on the server:

 $.get('/foo.txt', function(result) { if (result == 'ON') { alert('ON'); } else if (result == 'OFF') { alert('OFF'); } else { alert(result); } }); 
+11
source

You can use $.get to capture the contents of this file, and then perform an action based on the returned data:

 $.get('/path/to/file.txt',function(data) { if (data == "ON") { } else { } }); 
+6
source

You can also use this file as a configuration (for future development) and store more information there, for example:

yourfile.txt:

 {"enable":"ON","version":"1.0"} 

and additionally use JSON to analyze the contents of the file:

 $.get('/path/yourfile.txt', function(data) { var config = JSON.parse(data); if(config.enable == 'ON') { // ... } // ... if(config.version == '1.0') { // ... } // ... }); 
+1
source

Source: https://habr.com/ru/post/889976/


All Articles