【发布时间】:2019-12-16 09:06:57
【问题描述】:
概述
从我的网站向运行 CherryPy 的 Python 服务器创建发布请求时,我收到错误 Access to XMLHttpRequest has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response. 。我可以使用“CORS Everywhere”浏览器扩展之一暂时解决这个问题,但是
- 由于最近的更新,这些扩展尚未更新以再次工作。
- 所涉及的网站最终需要在我的本地综合体中被许多人使用,而无需浏览器扩展,因此一旦扩展更新,无论哪种方式都没有关系,因为我不能依赖这些扩展,并强迫所有人使用它们(当显然存在不需要扩展的修复时)。 我认为这些解决方案可能已经过时,但不确定。
以下是相关代码:
在服务器端(CherryPy/Python):
从网站发布请求调用的 CherryPy Python 函数
@cherrypy.expose
@cherrypy.tools.json_in()
def add_meeting(self):
data = None
id = None
start_time = None
end_time = None
title = None
userlist = None
result = {"operation": "request", "result": "success"}
if cherrypy.request.method == "POST":
data = cherrypy.request.json
id = data["id"]
start_time = data["start_time"]
end_time = data["end_time"]
title = data["title"]
userlist = data["userlist"]
# Rest of relevant code in function is left out, to take up less
# space and not post irrelevant code. That being said, I am
# positive the logic is correct, as it originally ran smoothly
# with a "Cors Everywhere" Browser Extension.
return result
这里是我设置和运行 CherryPy 的区域
def main():
# Create the configuration file parser object and start the CherryPy server
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)
port = config.getint('Meta', 'port')
host = config.get('Meta', 'host')
cherrypy.config.update({'server.socket_port': port,
'server.socket_host': host,
'tools.CORS.on': True})
cherrypy.quickstart(Coordinator(config))
main()
这是上面代码中提到的配置文件(CONFIG_FILE)
[Meta]
host = 0.0.0.0
port = 3000
# Rest is left out, as it is irrelevant with problem
我尝试实施的解决方案
- 在 main 函数之上包含以下函数:
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
with cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
2.在上面的cherrypy.config.update中添加“'cors.expose.on': True”
3.使用我在网上找到的这个cherrypy-cors Python库:https://pypi.org/project/cherrypy-cors/
4. 在 Python 文件的 config.update 部分中包含标头
5. 在“def add_meeting”前添加“@cherrypy.tools.accept(media='application/json')”
结论
我已经分别尝试了上述解决方案,一些有和没有其他的,但我仍然卡住了。也许其中一些解决方案是部分正确的,我的代码还需要一些额外的东西。我不知道;我只是无法让它工作。在此之前我没有太多的 Web 开发经验,所以也许(并且希望)解决方案非常简单。我知道代码有效,但如果没有为每个用户提供有效的“Cors Everywhere”浏览器扩展程序,我就无法运行它。
至于我正在运行的版本:我使用的是 CherryPy 14.2.0 和 Python 2.7.6
任何帮助对我来说都意味着绝对的世界,谢谢。
【问题讨论】:
-
@JammyDodger 非常感谢您的回复。不幸的是,所有服务器都应该在 Python CherryPy 上运行,虽然这个解决方案中使用的许多想法都是我一直在尝试实现的(在 CherryPy 中,不像那个特定链接),但在 Python CherryPy 中实现它一直是恶梦。问题在于让它在 CherryPy 上运行。
-
@webKnjaZ 我尝试通过在上面的“cherrypy.config.update”中包含“cors.expose.on”以及添加“cherrypy_cors.install()”行来实现cherrypy-cors在它上面,通过pip安装后,我仍然有同样的问题。不久前我尝试了这个解决方案,但它似乎不起作用。不过,感谢您的回复。
-
@webKnjaZ 非常感谢您花时间帮助我解决这个问题。你成就了我的夏天。感谢您耐心地教我一些 StackOverflow 约定。干杯。