javascript - 添加http响应头

标签 javascript html heroku

我已经用 python 在 Heroku 中编写了一个 Web 应用程序,并希望授予 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” Hook like this .

关于javascript - 添加http响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48046789/

相关文章:

javascript - navigator.platform 在 ARM Mac 上的值(value)是什么?

jQuery 对话框覆盖在不同 div 不透明度下改变位置

javascript - 使用 JavaScript 获取所有输入对象的列表,无需访问表单对象

reactjs - Create-React-App/React Router 无法通过渲染进行部署

python - 将 YAML 转换为 JSON 时出错 : yaml: line 3: did not find expected key

ruby-on-rails - rails 中的线程会自动打开数据库连接吗?

javascript - 在Webstorm中表示一个函数被外部​​使用

javascript - jquery 不在 chrome 中运行,在 ff 中运行

javascript - 如何在 Angular2 中嵌入视频?

javascript - 抵押贷款摊销付款计算