【发布时间】:2019-01-29 20:40:18
【问题描述】:
特别是,我想为 4xx 和 5xx 添加 CORS 标头,以便我的前端 web 应用程序可以向用户显示错误信息。
在我的应用程序中,我有一个 root 资源,我使用 putChild 添加叶资源。例如:
root = Root()
proxy = Proxy()
root.putChild("".encode('utf-8'), Root())
root.putChild("proxy".encode('utf-8'), proxy)
proxy.putChild("listMonitors".encode('utf-8'), ListMonitors())
proxy.putChild("getMonitorValues".encode('utf-8'), GetMonitorValues())
proxy.putChild("setStartInstrument".encode('utf-8'), SetStartInstrument())
proxy.putChild("setStopInstrument".encode('utf-8'), SetStopInstrument())
proxy.putChild("setPowerOnInstrument".encode('utf-8'), SetPowerOnInstrument())
proxy.putChild("setPowerOffInstrument".encode('utf-8'), SetPowerOffInstrument())
site = server.Site(root)
This 似乎与 Twisted 文档相关,并且可能允许在响应中设置标题,但我不确定如何应用它。我是否需要放弃 putChild 方法,转而让我的 root 资源直接流量到我的所有叶子资源或 noresource 用于 404 错误?其他错误类型呢?
更新:
一位评论者要求了解Root 是什么:
class Root(resource.Resource):
isLeaf = False
def render_GET(self, request):
request.setHeader('Access-Control-Allow-Origin', '*')
return "arkanoid?".encode()
【问题讨论】:
-
什么是
Root?
标签: cors twisted twisted.web