1. 程式人生 > >利用中介軟體解決跨域

利用中介軟體解決跨域

from django.utils.deprecation import MiddlewareMixin

class Mymiddle(MiddlewareMixin):
    def process_response(self,request,response):
        response["Access-Control-Allow-Origin"] = "*"
        if request.method == "OPTIONS":
            # 複雜請求會先發預檢
            response["Access-Control-Allow-Headers
"] = "Content-Type" response["Access-Control-Allow-Methods"] = "PUT,PATCH,DELETE" return response

然後在setting中註冊這個中介軟體