一、Django
打开settings.py文件
1.安装django-cors--headers
命令pip install django-cors-headers
2.找到INSTALLED_APPS
在INSTALLED_APPS的里面,自创APP的前面,输入:
'corsheaders',
3.找到MIDDLEWARE
在'django.middleware.common.CommonMiddleware',之前输入'corsheaders.middleware.CorsMiddleware'
4.跨域配置
#跨域允许的请求方式
CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
#设置跨域的请求头
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
#跨域请求时,是否运行携带cookie
CORS_ALLOW_CREDENTIALS = True
#允许所有的主机执行跨站点请求
CORS_ORIGIN_ALLOW_ALL = True
二、uni-app
打开mainfest.json文件
在后面输入:
"h5": {
"devServer": {
"proxy": {
"/8888": {
"target": "https://www.baidu.com/api",
"changeOrigin": true,
"pathRewrite": {
"^/8888": "/"
}
},
"/8800": {
"target": "https://www.taobao.com/api",
"changeOrigin": true,
"pathRewrite": {
"^/8800": ""
}
}
}
}}