[英]adding http response header
I have written a web app in Heroku in python and would like to grant javascript clients basic access to my resources. 我已经在Heroku中使用python编写了一个网络应用,并希望向javascript客户端授予对我的资源的基本访问权限。 I have been reading this article on how to do that: https://www.w3.org/wiki/CORS_Enabled 我一直在阅读有关如何执行此操作的文章: https : //www.w3.org/wiki/CORS_Enabled
and from the article I have found that I should do the following: 从这篇文章中我发现我应该执行以下操作:
print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")
procfile is as follows: web: python app.py
procfile如下: web: python app.py
and app.py is as follows: 和app.py如下:
#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
@bottle.route('/')
def index():
print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")
return("testing")
bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))
however I still can't access the resources, I am getting this error: 但是我仍然无法访问资源,出现此错误:
Failed to load https://ksmlgames.herokuapp.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
thanks for the help 谢谢您的帮助
#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
@bottle.route('/')
def index():
response.set_header("Content-Type", "text/turtle")
response.set_header("Content-Location", "mydata.ttl")
response.set_header("Access-Control-Allow-Origin", "*")
return("testing")
bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))
@thmsdnnr, This seems to be working, thanks @thmsdnnr,这似乎正在工作,谢谢
You'll want to use response.set_header . 您将要使用response.set_header 。
def index():
response.set_header("Content-Type", "text/turtle")
response.set_header("Content-Location", "mydata.ttl")
response.set_header("Access-Control-Allow-Origin", "*")
return("testing")
If you find yourself doing this for many routes, you can set up an 'after_request' hook like this . 如果您发现自己在许多路线上都这样做,则可以像这样设置一个“ after_request”挂钩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.