周末,Chrome似乎已经更新,现在阻止了我的跨域请求:
我有一个服务器,两个域和一个共享的web图标字体,需要加载另一个。我不想强迫Cherrypy知道它所服务的域(镜像或主域),因为为了这个努力,我可以克隆所有代码。
我的chrome console.log错误消息是:
Font from origin 'http://djotjog.com' has been blocked from loading by Cross-Origin Resource Sharing policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://storylearning.org' is therefore not allowed access.
在cherrypy中我尝试启用此功能,但文档很粗略。这是我试过的:
class root:
def stest(self, **kw):
cherrypy.response.headers['Content-Type'] = 'text/html'
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
html = some_function()
return html
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
if __name__=="__main__":
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
cherrypy.config.update({
'environment': 'production',
'log.screen': False,
'log.error_file':'cperror.log',
'server.socket_host': '127.0.0.1',
'server.socket_port': 15722,
'server.thread_pool': 2,
'server.thread_pool_max': 2
那么处理cherrypy上的跨域请求的正确方式是什么?
答案 0 :(得分:1)
首先,如果你说它是一面镜子,为什么会有这个问题还不清楚。如果它真的是一面镜像,那么你只需要使用相对URL(/path/to/your/font.otf)来引用你的字体文件。
其次,自2010年起,网络字体至少在FF和IE中受到CORS的约束(参见bugreport)。而行为实际上是设计的(见spec)。因此,如果您曾在这些浏览器中测试过您的网站,那么您应该看到同样的事情。
第三,如果您正在谈论部分镜像,那么以下内容应该满足您的需求。
app.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
from cherrypy.lib import static
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
},
'/font' : {
'tools.corsstaticdir.on' : True,
'tools.corsstaticdir.dir' : os.path.join(path, 'font'),
# This is a workaround for CherryPy flaw where they set ``section``
# tool config variable in ``cherrypy._cpdispatch.Dispatcher.set_conf``
# exclusivety for ``staticdir`` tool (http://goo.gl/EGIIvA).
'tools.corsstaticdir.section' : '/font',
}
}
def corsstaticdir(section, dir, root = '', match = '', content_types = None, index = '', debug = False):
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
return static.staticdir(section, dir, root, match, content_types, index, debug)
cherrypy.tools.corsstaticdir = cherrypy._cptools.HandlerTool(corsstaticdir)
class App:
@cherrypy.expose
def index(self):
return static.serve_file(os.path.join(path, 'index.html'))
if __name__ == '__main__':
cherrypy.quickstart(App(), '/', config)
的index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'/>
<title>CORS font</title>
<style type='text/css'>
@font-face {
font-family : YourFontRegular;
font-weight : normal;
font-style : normal;
src : url('http://anotherdomain/font/Quicksand-Regular.otf');
}
p {
font-family : YourFontRegular;
}
</style>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam placerat lacinia tortor. Nulla viverra, dolor nec malesuada congue, lectus quam dictum tellus, at malesuada nisl tortor quis ligula. Donec aliquam id arcu quis consectetur. Sed vitae turpis quis metus consequat vulputate.</p>
</body>
</html>