Kwarunek's answer led me to solve my problem with a PUT and DELETE request. The only solution is overly suitable for the example with GET and POST. In this case, the line
self.set_header("Access-Control-Allow-Origin", "*")
actually enough (unless the browser blocks CORS in front of everyone). This is most relevant for PUT and DELETE queries. What happens here at the network level can be a bit more complicated than with GET / POST.
"If the request is a" difficult "request, the browser first sends the OPTIONS request without a preview to make sure the server accepts the request. The request is not simple when using an HTTP verb other than GET or POST (for example, PUT, DELETE)." cf. difficult requests
class BaseHandler(tornado.web.RequestHandler): def set_default_headers(self): print("setting headers!!!") self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Access-Control-Allow-Headers", "x-requested-with") self.set_header('Access-Control-Allow-Methods', ' PUT, DELETE, OPTIONS') def options(self):
Now all the handlers that inherit from BaseHandler are fully compatible with CORS:
class MyHandler(BaseHandler): def put(self): self.write('some post') def delete(self): self.write('some get')
source share