django 解决跨域问题

徘徊边缘 提交于 2019-12-20 00:47:15
如果不想建立新的middleware 就在默认的或新增的AuthenticationMiddleware下面新增process_response函数(视图函数执行后的一些操作)

以下为部分代码

 def process_response(self, request, response):
        try:
            ip = request.META.get('HTTP_ORIGIN')
            response["Access-Control-Allow-Origin"] = ip
            response["Access-Control-Allow-Credentials"] = 'true'
            response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
            response["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept"
            response["Access-Control-Max-Age"] = "1728000"
            response["Access-Control-Expose-Headers"] = 'Date'
        except Exception as e:
        	print(e)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!