【问题标题】:unable to access a .csv file using java script in html无法在 html 中使用 javascript 访问 .csv 文件
【发布时间】:2013-09-28 06:08:45
【问题描述】:

我试图掌握d3.js 以获取可视化数据表示,并且在其中提供的教程之一中,我尝试运行其中一个将 .csv 文件作为输入的代码,然后使用所需的 svg 方法和在网页上显示它的 java 脚本。

好吧,当我尝试运行以下代码时,它没有成功。我的系统中安装了 xampp。我将 j3s.html 保存在 httpdocs 文件夹中,并将 .csv 文件保存在同一个文件夹中,但是一些它是如何不工作的。

我对 web 和 java 脚本完全陌生,但是这段代码很容易理解。下面是代码。

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<body>
    <div id="viz"></div>
    <script type="text/javascript">

d3.text("auto_mpg_tmp.csv", function(datasetText) {

var parsedCSV = d3.csv.parseRows(datasetText);

var sampleHTML = d3.select("#viz")
    .append("table")
    .style("border-collapse", "collapse")
    .style("border", "2px black solid")

    .selectAll("tr")
    .data(parsedCSV)
    .enter().append("tr")

    .selectAll("td")
    .data(function(d){return d;})
    .enter().append("td")
    .style("border", "1px black solid")
    .style("padding", "5px")
    .on("mouseover", function(){d3.select(this).style("background-color", "aliceblue")})
    .on("mouseout", function(){d3.select(this).style("background-color", "white")})
    .text(function(d){return d;})
    .style("font-size", "12px");
});

    </script>
</body>
</html>

好吧,我的 XAMPP 也有很多问题,我尝试使用 this link 解决这些问题。 这是xampp服务器的错误日志。

4:17:41 PM  [Apache]    Error: Apache shutdown unexpectedly.
4:17:41 PM  [Apache]    This may be due to a blocked port, missing dependencies, 
4:17:41 PM  [Apache]    improper privileges, a crash, or a shutdown by another method.
4:17:41 PM  [Apache]    Press the Logs button to view error logs and check
4:17:41 PM  [Apache]    the Windows Event Viewer for more clues
4:17:41 PM  [Apache]    If you need more help, copy and post this
4:17:41 PM  [Apache]    entire log window on the forums

【问题讨论】:

    标签: javascript google-chrome d3.js xampp


    【解决方案1】:

    我不确定您的服务器堆栈有什么问题。但是 Javascript 代码似乎很好。我有它running intact on JSBin。

    【讨论】:

      【解决方案2】:

      对您来说最简单的方法是使用 Python 网络服务器进行开发,而不是使用整个 XAMPP 堆栈。

      确保已安装 Python。在包含您的顶级index.html(您在上面粘贴的内容)的目录中,只需执行以下操作:

      python -m SimpleHTTPServer
      

      这将启动一个迷你网络服务器,您可以通过在浏览器中输入以下内容来访问它:

      http://127.0.0.1:8000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-21
        • 2021-12-08
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2018-06-10
        • 2020-10-05
        • 1970-01-01
        相关资源
        最近更新 更多