0
点赞
收藏
分享

微信扫一扫

项目中浏览器发送请求自动携带Cookie

幺幺零 2022-04-13 阅读 93
前端

我们要在请求头中添加上这个配置:
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 => {})
举报

相关推荐

0 条评论