从文件中读取数据并在浏览器上显示

reading data from file and displaying on browser

本文关键字:浏览器 显示 数据 文件 读取      更新时间:2023-09-26

我是javascript的新手(主要在后端工作),但现在我想创建一个可视化,并希望使用javascript。

我将数据保存在格式的文件中

id1 uid1 rating
id1 uid2 rating2

刚开始,我想从这个文件中读取数据并在浏览器上显示它吗?我需要启动服务器吗。。或者我可以就这样做吗。

如有任何建议/指示,我们将不胜感激。THanks

您需要了解ajax、以及处理浏览器差异。一种在firefox和chrome上工作的方法是:

  <body>
  <div id="log"></div>
    <script type="text/javascript">
    var request = new XMLHttpRequest();//depends on the browser , several ways to create the object
    request.onreadystatechange = function(e){
      if(request.readyState == 4 && request.status==200){
        // do whatever you need with the data
        document.getElementById("log").innerText = request.responseText;
      }
    }
    // assuming the file is called data and is located on current-path-on-server/data
    request.open("GET","data",true);
    request.send();
  </script>  
  </body>

更多信息:

https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started