前后端分离产生的跨域问题

前后端分离产生的跨域问题

产生的原因:

​ 由于浏览器的同源策略

解决方案

  • 1 jsonp

  • 2 cors

    添加响应头解决,可自己写django中间件,也可以使用第三方模块django-cors-headers

    配置参考

    自己实现的cors中间件

    class CORSMiddleware(MiddlewareMixin):
        
        def process_responce(self,request,response):
            # 允许域名来获取数据
            response['Access-Control-Allow-Origin'] = "*"
            # 允许携带Content-Type请求头
            response['Access-Control-Allow-Headers'] = "Content-Type"
            # 允许发送的请求方式
            response['Access-Control-Allow-Methods'] = "DELETE,PUT"
            
            return response
    
posted @ 2019-03-22 18:22  sh4on  阅读(638)  评论(0编辑  收藏  举报