最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read from textfile on server using jquery - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jun 7, 2011 at 17:24 prometheuspkprometheuspk 3,81511 gold badges45 silver badges59 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

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

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 {

   }
});

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') {
         // ...
    } // ...
});
发布评论

评论列表(0)

  1. 暂无评论