问题描述
欲从中距离d3的目录不同的特定目录加载TSV文件。我的D3脚本驻留在 /主页/大会/工作区/ d3_test /脚本
和SimpleHTTPServer运行在/根目录下。
d3.tsv(HTTP://本地主机:8888 /家居/大会/数据/ data.tsv功能(错误数据){
的console.log(取data.tsv文件+误差+数据);
});
抛出一个错误:
GET的http://本地主机:8888 /家居/大会/数据/ data.tsv 200 OK 5毫秒(在红色)
取data.tsv文件[对象XMLHtt prequest]未定义
注: -
- 在试图通过
文件:///
不支持 - 迈克提到与SimpleHTTPServer链接
- 另外,我不希望我的编辑浏览器设置
我结束了正在运行的服务器的根目录/
与蟒蛇-m SimpleHTTPServer 8888&安培;
从本地主机访问我的HTML文件
有关如 - 的http://本地主机:8888 /家居/大会/工作区/ d3_test /脚本/ data_fetch.html
和我的TSV文件在以下位置
的http://本地主机:8888 /家居/大会/数据/ data.tsv
注: - 设置您的SimpleHTTPServer在根/
帮助您从任何目录,在给定的安全费用提取任何文件
编辑: -
不幸的是,SimpleHTTPServer尤其是发送头的也不是那么简单的,它不允许任何定制。然而,你可以自己创建一个简单的HTTP服务器,采用最SimpleHTTPServer的,只是添加所需的标题。
只需创建一个文件simple-cors-http-server.py(或其他),并把里面的以下内容:
#!在/ usr /斌/包膜python2
从SimpleHTTPServer进口SimpleHTT prequestHandler
进口BaseHTTPServer
类CORSRequestHandler(SimpleHTT prequestHandler):
高清end_headers(个体经营):
self.send_header(访问控制 - 允许 - 原产地','*')
SimpleHTT prequestHandler.end_headers(个体经营)
如果__name__ =='__main__':
BaseHTTPServer.test(CORSRequestHandler,BaseHTTPServer.HTTPServer)
然后,你可以做蟒蛇simple-cors-http-server.py,它会推出,这将设置CORS头为每个响应修改的服务器。
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:-
- Tried passing
file:///
which is not supported - Mike mentions link with SimpleHTTPServer
- Also, I don't want to edit my browser settings
I end up running server at root "/"
with python -m SimpleHTTPServer 8888 &
accessing my html file from localhost
for e.g. - http://localhost:8888/home/meet/workspace/d3_test/scripts/data_fetch.html
and my tsv file is on following location
http://localhost:8888/home/meet/data/data.tsv
Note:- Setting up your SimpleHTTPServer at root "/"
helps you to fetch any file from any directory at given security cost.
EDIT:-
Unfortunately, SimpleHTTPServer is really that simple that it does not allow any customization, especially not of the headers it sends. You can however create a simple HTTP server yourself, using most of SimpleHTTPServer and just add that desired header.
Simply create a file simple-cors-http-server.py (or whatever) and put the following inside:
#! /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)
Then you can do python simple-cors-http-server.py and it will launch your modified server which will set the CORS header for every response.
这篇关于D3不支持跨域请求,即使使用SimpleHTTPServer后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!