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

时间:2013-02-02 00:10:12

标签: javascript html

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

我将数据保存在格式

的文件中
id1 uid1 rating
id1 uid2 rating2

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

任何建议/指示将不胜感激。 感谢

1 个答案:

答案 0 :(得分:1)

您需要了解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

相关问题