[英]adding http response header
我已經在Heroku中使用python編寫了一個網絡應用,並希望向javascript客戶端授予對我的資源的基本訪問權限。 我一直在閱讀有關如何執行此操作的文章: https : //www.w3.org/wiki/CORS_Enabled
從這篇文章中我發現我應該執行以下操作:
print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")
procfile如下: web: python app.py
和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))
但是我仍然無法訪問資源,出現此錯誤:
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.
謝謝您的幫助
#!/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,這似乎正在工作,謝謝
您將要使用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")
如果您發現自己在許多路線上都這樣做,則可以像這樣設置一個“ after_request”掛鈎。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.