关于前后端分离的跨域问题
当前环境:
- umijs 使用dva/fetch库
- python REST api
- nginx 服务器
- 前端域名:www.test.com
- 后端域名:api.test.com
后端处理:
// 此处填写前端使用的域名
add_header Access-Control-Allow-Origin http://www.test.com;
// 表示允许使用cookie
add_header Access-Control-Allow-Credentials true;
前端处理:
由于fetch默认不携带cookie
// 带上允许使用cookie的参数
fetch(url, {credentials: 'include'});