Read from textfile on server using jquery
Solution 1
You could use the $.get
method to send a GET AJAX 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);
}
});
Solution 2
You can use $.get
to grab the content of that file, and then take action based on the data returned:
$.get('/path/to/file.txt',function(data) {
if (data == "ON") {
} else {
}
});
Solution 3
You can also use this file as a config (for future development) and keep there more informations, for example:
yourfile.txt:
{"enable":"ON","version":"1.0"}
and additionally use JSON
to parse file content:
$.get('/path/yourfile.txt', function(data) {
var config = JSON.parse(data);
if(config.enable == 'ON') {
// ...
} // ...
if(config.version == '1.0') {
// ...
} // ...
});
prometheuspk
Out of the night that covers me, Black as the pit from pole to pole, I thank whatever gods may be For my unconquerable soul. In the fell clutch of circumstance I have not winced nor cried aloud. Under the bludgeonings of chance My head is bloody, but unbowed. Beyond this place of wrath and tears Looms but the Horror of the shade, And yet the menace of the years Finds and shall find me unafraid. It matters not how strait the gate, How charged with punishments the scroll, I am the master of my fate: I am the captain of my soul. Invictus - William Ernest Henley
Updated on July 28, 2022Comments
-
prometheuspk almost 2 years
I have a textfile (.txt) on a server. It contains only one word i.e. ON or OFF. How can i use JQuery or Pure Javascript to read that word from the file.
I am thinking along the lines of
$.ajax.
-
steffen over 6 yearsif the file contains json, let the extension be
.json