Javascript:获取本地服务器端文件的内容 [英] Javascript: Getting the contents of a local server-side file

查看:184
本文介绍了Javascript:获取本地服务器端文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web服务器(test.txt)上有一个文件,我需要获取它的内容,然后将它分配给一个变量,以便进一步操作并最终输出到页面,这应该每隔一秒左右发生一次(因此为什么PHP不是真正的选择)。



我试过使用jQuery get,XMLHttpRequest和Ajax - 都没有运气。如果有人可以告诉我如何解决我的问题,我会非常感激。

您可以加载以这种方式你的文件内容 -

  var txt =; 

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

对于每秒钟左右的加载,

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


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.

解决方案

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)

这篇关于Javascript:获取本地服务器端文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆