Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用D3库时的XMLHttpRequest问题_Javascript_D3.js - Fatal编程技术网

Javascript 使用D3库时的XMLHttpRequest问题

Javascript 使用D3库时的XMLHttpRequest问题,javascript,d3.js,Javascript,D3.js,我目前正在尝试运行一个名为的简单库。这是d3库,我非常兴奋使用,但我有一些问题,让它运行。它看起来像是一个非常简单的库,但我经常出错。我有以下资料: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Testing</title> <link href="lib/metricsgraphics.css" rel="styl

我目前正在尝试运行一个名为的简单库。这是d3库,我非常兴奋使用,但我有一些问题,让它运行。它看起来像是一个非常简单的库,但我经常出错。我有以下资料:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Testing</title>
    <link href="lib/metricsgraphics.css" rel="stylesheet">
  </head>
  <body>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.js"></script>
    <script src="lib/metricsgraphics.js"></script>
    <script type="text/javascript">
      d3.json('data/ufo-sightings', function(data) {
      })

      d3.json('data/ufo-sightings', function(data) {
        data = MG.convert.date(data, 'year');

      })

      d3.json('data/ufo-sightings', function(data) {
            MG.data_graphic({
                title: "UFO Sightings",
                description: "Yearly UFO sightings from the year 1945 to 2010.",
                data: data,
                width: 650,
                height: 150,
                target: '#ufo-sightings',
                x_accessor: 'year',
                y_accessor: 'sightings',
                markers: [{'year': 1964, 'label': '"The Creeping Terror" released'}]
            })
        })
    </script>
  </body>

有人能帮我解释并提供一个可能的解决方案吗?

您直接在浏览器中运行源代码,最终向文件url发出JSON请求,这在浏览器的安全约束下是不允许的,错误告诉您。虽然您可以通过威胁不同的浏览器来实现这一点,但一个更安全、更简单的解决方案是设置您的开发环境,以便通过http提供服务

XMLHttpRequest cannot load file:///directory/where/my/data/is. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load file:///directory/where/my/data/is.