繁体   English   中英

添加http响应头

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM