[目标]
CORS(跨源资源共享Cross-Origin Resource Sharing)
当使用者代理请求一个不是目前文件来源
例如来自于不同通讯协定(protocol)、网域(domain)以及通讯埠(port)会建立一个跨来源 HTTP 请求(cross-origin HTTP request)。
参考下面文章解释,有兴趣可以自己深入了解
CORS(跨源资源共享Cross-Origin Resource Sharing)
同源政策 (Same-origin policy)
所谓同源是指两份网页具备相同通讯协定(protocol)、网域(domain)以及通讯埠(port)
1. 安装CORS套件
pip install django-cors-headers
2. Code 撰写
settings.pyINSTALLED_APPS = [ . 'corsheaders', .]MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', . .]CORS_ORIGINS_WHITELIST = [ "localhost:4200",]CORS_ORIGIN_ALLOW_ALL = TrueCORS_ALLOW_METHODS = [ 'DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT']CORS_ALLOW_HEADERS = [ "accept", "accept-encoding", "authorization", "content-type", "dnt", "origin", "user-agent", "x-csrftoken", "x-requested-with",]
上一篇
下一篇