2013-02-02 101 views
0

我是javascript新手(主要工作在後端),但現在我想創建一個可視化文件,並希望爲其使用JavaScript。從文件中讀取數據並在瀏覽器上顯示

我已經保存在一個文件中的數據格式

id1 uid1 rating 
id1 uid2 rating2 

剛上手,我想從這個文件中讀取數據,並在我的瀏覽器顯示呢? 我是否需要啓動服務器..或者我可以這樣做。

任何建議/方向將不勝感激。 THanks

回答

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

相關問題