后端报错信息
WARNING:tornado.access:405 OPTIONS /add (::1) 1.00m
前端报错信息
2xhr.Js?ec6c:172 OPTIONS http://localhost:8888/add 405 (Method Not Allowed)/#/:1 Access to XMLhttpRequest at ‘http://localhost:8888/add‘ from origin ‘http://localhost:8080‘ has been blocked by CORS policy: Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
解决办法
在代码中实现options方法并设置self.set_header(‘Access-Control-Allow-headers‘,‘Content-Type‘)
import tornado.ioloopimport tornado.webimport Jsonclass AddHandler(tornado.web.RequestHandler): def initialize(self): # self.request.method = ‘POST‘ # print(self.request.method,type(self.request.method)) self.set_default_header() def get(self): self.write("U get story ID is ") def post(self): self.write(Json.dumps({"sum": ‘rrrrrr‘})) def set_default_header(self): print("setting headers!!!") self.set_header(‘Access-Control-Allow-Origin‘,‘*‘) # self.set_header(‘Access-Control-Allow-Origin‘,‘http://localhost:8080‘) # self.set_header(‘Access-Control-Allow-headers‘,‘X-Requested-With‘) self.set_header(‘Access-Control-Allow-headers‘,‘*‘) self.set_header(‘Access-Control-Allow-Methods‘,‘GET,POST,PUT,DELETE,PATCH,OPTIONS‘) self.set_header(‘Content-Type‘,‘application/Json; charset=UTF-8‘) self.set_header(‘Access-Control-Allow-headers‘,‘Content-Type‘) def options(self): passapplication = tornado.web.Application([ (r"/add",AddHandler),])if __name__ == "__main__": application.Listen(8888) tornado.ioloop.Ioloop.instance().start()
参考网址
https://github.com/tornadoweb/tornado/issues/2104
https://stackoverflow.com/questions/19006783/tornado-post-405-method-not-allowed?noredirect=1&lq=1
总结以上是内存溢出为你收集整理的tornado接收ajax的post请求报错WARNING:tornado.access:405 OPTIONS /add全部内容,希望文章能够帮你解决tornado接收ajax的post请求报错WARNING:tornado.access:405 OPTIONS /add所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)