【问题标题】:SimpleHTTPServer code 404, message File not foundSimpleHTTPServer 代码 404,消息文件未找到
【发布时间】:2013-11-11 08:34:53
【问题描述】:

我正在尝试运行一个简单的 d3 Javascript 程序来可视化图表。我也有这个图的 JSON 文件。为了让程序运行,我被告知我应该遵循以下步骤:

1- 在终端上,我转到项目所在的文件夹。
2- 我插入以下命令:python -m SimpleHTTPServer 8888 &
3- 在网络浏览器 (Firefox) 上,我添加了这个:http://localhost:8888

当我执行第三步时,终端显示以下错误消息:

localhost - - [11/Nov/2013 08:07:23] code 404, message File not found
localhost - - [11/Nov/2013 08:07:23] "GET /D3/sample.json HTTP/1.1" 404 -

这是我的 d3 Javascript 图表的 HTML 文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<style>

.node {
   stroke: #fff;
   stroke-width: 1.5px;
}

.link {
   stroke: #999;
   stroke-opacity: .6; 
}

</style> 
<body>
<p> Paragraph !!! </p>
<script type="text/javascript" src="d3.v3.js"></script>
<script>

var width = 960,
height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height);

d3.json("sample.json", function(error, graph) {
force
  .nodes(graph.nodes)
  .links(graph.links)
  .start();

var link = svg.selectAll(".link")
  .data(graph.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value); });

var node = svg.selectAll(".node")
  .data(graph.nodes)
  .enter().append("circle")
  .attr("class", "node")
  .attr("r", 5)
  .style("fill", function(d) { return color(d.group); })
  .call(force.drag);

node.append("title")
  .text(function(d) { return d.name; });

force.on("tick", function() {
   link.attr("x1", function(d) { return d.source.x; })
   .attr("y1", function(d) { return d.source.y; })
   .attr("x2", function(d) { return d.target.x; })
   .attr("y2", function(d) { return d.target.y; });

node.attr("cx", function(d) { return d.x; })
   .attr("cy", function(d) { return d.y; });
});
});

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

似乎无法读取 JSON 文件 sample.json 上面显示的消息。谁能帮助我如何运行该程序并使用我上面提供的命令读取 json 文件。如果我在该 HTML 文件中添加标题和段落,它们会出现但无法显示图表。 JSON文件的位置有问题还是d3.v3.js文件有问题?提前感谢您的帮助。

`

【问题讨论】:

  • 我遇到了类似的 404 错误,由于某种原因,服务器不接受我的实际目录路径。所以我删除了路径并将.js库文件放在html文件所在的同一目录中。那行得通。

标签: javascript python json apache d3.js


【解决方案1】:

据我了解,您已经在一个目录中设置了一个 python 简单服务器,并且在该目录中您有一个在浏览器中显示的 html 文件。但是,当您尝试运行 js 代码并加载 json 文件时,您会收到 404 错误。

错误说它正在一个名为 D3 的目录中查找 json 文件,但是,您的代码正在根目录中查找 json。尝试更改

D3.json("sample.json", function(error, graph)

行到

d3.json("D3/sample.json", function(error, graph)。

另外,在函数调用的地方console.log(graph)里面是这样的:

d3.json("sample.json", function(error, graph) {
    console.log(graph)

这会将输出发送到您的控制台,以便您检查正在读取的内容(如果您已经知道,请道歉)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多