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

人走茶凉 提交于 2019-12-25 18:34:16

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/16123323/javascript-getting-the-contents-of-a-local-server-side-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!