加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

tornado接收ajax的post请求报错WARNING:tornado.access:405 OPTI

发布时间:2020-12-20 10:30:59 所属栏目:Python 来源:网络整理
导读:后端报错信息 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:

后端报错信息

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.ioloop
import tornado.web
import json


class 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):
        pass


application = 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读