一般状况下,A网页访问B服务器资源时,不知足如下三个条件其一就是跨域访问
python
协议不一样
端口不一样
主机不一样
1.安装django-cors-headers模块django
pip3 install django-cors-headers
跨域
2.注册AAP服务器
INSTALLED_APPS = [ ... 'corsheaders' ]
3.添加中间件cors
MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware' ]
settings.py
中配置code
1.容许全部来源访问中间件
CORS_ORIGIN_ALLOW_ALL = True
ip
2.容许部分来源访问资源
CORS_ORIGIN_ALLOW_ALL = False
get
CORS_ORIGIN_WHITELIST = [
'http://example.com' #容许访问的来源]
设置指定来源注意点
:
http://127.0.0.1:1000
你发起请求时http://localhost:1000
数据就无法得到