2

I want to load a tsv file from a specific directory which is different from the d3 directory. My d3 script resides under /home/meet/workspace/d3_test/scripts and SimpleHTTPServer running at "/" root.

d3.tsv("http://localhost:8888/home/meet/data/data.tsv",function(error,data){ 
    console.log("fetching data.tsv file "+error+data);
    });

Throwing an error:

GET http://localhost:8888/home/meet/data/data.tsv 200 OK 5ms (in red color)
fetching data.tsv file [object XMLHttpRequest] undefined

Note:-

  1. Tried passing file:/// which is not supported
  2. Mike mentions link with SimpleHTTPServer
  3. Also, I don't want to edit my browser settings
4

2 回答 2

2

我最终以根目录运行"/"服务器python -m SimpleHTTPServer 8888 &

从 localhost 访问我的 html 文件

例如 -http://localhost:8888/home/meet/workspace/d3_test/scripts/data_fetch.html

我的 tsv 文件位于以下位置

http://localhost:8888/home/meet/data/data.tsv

注意:- 在根目录设置 SimpleHTTPServer"/"可以帮助您以给定的安全成本从任何目录获取任何文件。

编辑:-

不幸的是,SimpleHTTPServer 真的那么简单,它不允许任何自定义,尤其是它发送的标头。但是,您可以自己创建一个简单的 HTTP 服务器,使用大部分 SimpleHTTPServer 并添加所需的标头。

只需创建一个文件 simple-cors-http-server.py (或其他)并将以下内容放入:

#! /usr/bin/env python2
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    BaseHTTPServer.test(CORSRequestHandler, BaseHTTPServer.HTTPServer)

然后你可以做 python simple-cors-http-server.py ,它会启动你修改过的服务器,这将为每个响应设置 CORS 标头。

于 2014-03-24T20:30:59.603 回答
0

Doesn't seem like there's enough evidence to conclude that this is a cross-domain problem. The console log should be able to provide more details, but I'd guess that the problem is that the server isn't setting the mime type correctly for tab-separated values. According to the d3.js source code for tsv files the mime type should be "text/tab-separated-values". Should be easy enough to check this in the console.

于 2014-03-21T23:05:14.023 回答