我们要在请求头中添加上这个配置:
credentials: ‘include’。
我的项目使用的umi框架,代码如下
import request from 'umi-request';
import { message } from 'antd';
import lodash from 'lodash';
export default function (url: string, options?: any) {
return new Promise((resolve, reject) => {
const newOptions = {
...options,
credentials: 'include',
headers: {},
};
let apiUrl = url;
request(apiUrl, { ...newOptions })
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
如果自己项目中使用的是axios,那么需要配置
withCredentials: true,
const opts: any = {
url,
method: 'post',
headers: {
"Content-Type": "application/json;charset=utf-8",
},
baseURL: '',
data: { ...params },
timeout: 30000,
withCredentials: true,
responseType: 'json',
responseEncoding: 'utf8',
maxContentLength: 2000,
}
axios({ ...opts }).then(res => {})