class HelloWorld:
@cherrypy.expose
def hello(self):
return "Hello World!"
cherrypy.quickstart(的HelloWorld())
我做跨域请求:http://ip:port/hello失败了,然后我在方法问候之前添加了一个装饰器(在我的restful应用程序中成功运行):
def crossdomain(func):
def decorate(*args, **kwargs):
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST, HEAD, PUT, DELETE"
allow_headers = ["Cache-Control", "X-Proxy-Authorization", "X-Requested-With", "Content-Type"]
cherrypy.response.headers["Access-Control-Allow-Headers"] = ",".join(allow_headers)
return func(*args, **kwargs)
return decorate
class HelloWorld:
@cherrypy.expose
@crossdomain
def hello(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
浏览器抛出此异常: 选项http://192.168.1.236:1987/hello 500(内部服务器错误)
因为cherrypy找不到http方法“OPTIONS”。 但是如何让cherrypy自动发送OPTIONS请求?