prometheuspk
prometheuspk

Reputation: 3815

Read from textfile on server using jquery

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.

Upvotes: 8

Views: 13045

Answers (3)

Tomasz Dzięcielewski
Tomasz Dzięcielewski

Reputation: 3907

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') {
         // ...
    } // ...
});

Upvotes: 2

Fosco
Fosco

Reputation: 38526

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 {

   }
});

Upvotes: 7

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

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);
    }
});

Upvotes: 15

Related Questions