Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2151
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

30条回答
  •  旧时难觅i
    2020-11-21 07:13

    And one more answer in Django. To have a single view allow CORS from multiple domains, here is my code:

    def my_view(request):
        if 'HTTP_ORIGIN' in request.META.keys() and request.META['HTTP_ORIGIN'] in ['http://allowed-unsecure-domain.com', 'https://allowed-secure-domain.com', ...]:
            response = my_view_response() # Create your desired response data: JsonResponse, HttpResponse...
            # Then add CORS headers for access from delivery
            response["Access-Control-Allow-Origin"] = request.META['HTTP_ORIGIN']
            response["Access-Control-Allow-Methods"] = "GET" # "GET, POST, PUT, DELETE, OPTIONS, HEAD"
            response["Access-Control-Max-Age"] = "1000"  
            response["Access-Control-Allow-Headers"] = "*"  
            return response
    

提交回复
热议问题