Search code examples
javascriptjquerygetserver-side

Javascript: Getting the contents of a local server-side file


I have a file on my web server (test.txt) and I need to get the contents of it, then assign it to a variable for further manipulation and eventual output to the page, and this should happen every second or so (hence why PHP isn't really an option).

I've tried using jQuery get, XMLHttpRequest and Ajax - all without luck. If somebody could advise me on how I need to go about solving my issue, I would greatly appreciate it.


Solution

  • You can load your file content in a var this way -

    var txt ="";
    
    $.get('test.txt', function (data) {
        txt = data;
    });
    

    For loading every second or so,

    setInterval(function(){
       $.get('test.txt', function (data) {
          txt = data;
       });
    },1000)