首页 1.10-跨域问题的解决
文章
取消

1.10-跨域问题的解决

Vue

重定向(vue.config.js)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Node.js里的模块
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true,
        pathRewrite: {
          '^api': ''
        }
      }
    }
  }
}

自定义请求头和携带上一次cookie(@/utils/ajax)

1
2
3
4
5
6
7
8
9
10
import axios from "axios"

export const ajax = axios.create({
    headers: {
        source: 'h5',  // 请求头1
        icode: 'acbd',  // 请求头2
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    withCredentials: true  // 是否携带上一次的cookie
})

异常处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ajax.interceptors.request.use(function (config) {
    // 发送请求之前做什么
    console.log('请求拦截到了')
    return config
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
})

ajax.interceptors.request.use(function (response) {
    // 对响应数据做点什么
    console.log('相应拦截到了')
    return response
}, function (error) {
    if (error.response.status === 401) {
        window.alert('未登录, 即将跳转到登陆页面')
    } else if (error.response.status === 500) {
        window.alert('服务器正忙,请稍后')
    }
    return Promise.reject(error)
})

将Vue赋值给window.app(main.js)

1
2
3
4
5
window.app = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

Django

安装第三方扩展

1
pip install django-cors-headers

配置(settings.py)

引用第三方扩展

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
	'corsheaders',
]

MIDDLEWARE = [
	'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',  // 在两者的中间添加
    'django.middleware.common.CommonMiddleware',
]
CORS跨域配置

请求白名单

1
2
3
4
CORS_ORIGIN_WHITELIST = (
    'http://127.0.0.1:8080',
    'http://localhost:8080'
)

请求头和cookie

1
2
3
4
# 允许携带哪些请求头
CORS_ALLOW_HEADERS = ('source', 'icode')
# 允许携带cookie
CORS_ALLOW_CREDENTIALS = True
本文由作者按照 CC BY 4.0 进行授权