Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AXIOS: How to remove cookies from a request 如何从请求中移除cookie #2472

Closed
qingyuun opened this issue Oct 17, 2019 · 3 comments
Closed

Comments

@qingyuun
Copy link

qingyuun commented Oct 17, 2019

Describe the issue

When I use the Axios request API interface, the background interface is set to carry cookies when I don't need the foreground to send the request. I set withcredentials to false but still carry cookies when I send the request. How can I solve this problem?

我在使用axios 请求api接口的时候,后台接口进行了相关设置,不需要前台发送请求时携带cookie ,我设置了 withCredentials 为false 但是在发送请求时仍然携带了cookie ,请问如何解决

Example Code
Code snippet to illustrate your question

//
const ajaxUrl = process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging' ? process.env.VUE_APP_API : undefined
axios.defaults.baseURL = ajaxUrl
// axios 配置
axios.defaults.timeout = 10000
axios.defaults.withCredentials = false
// request.withCredentials = true;
// 请求拦截器
axios.interceptors.request.use(
  config => {
    debugger
    // 根据相关情况修改默认url地址
    const url = config.url
    config.baseURL = url.indexOf('/easymock') !== -1 ? (process.env.NODE_ENV !== 'development' ? process.env.VUE_APP_API_Easy_Mock : '/') : process.env.NODE_ENV !== 'development' ? process.env.VUE_APP_API : '/'
    // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
    config.headers['Authorization'] = token.getToken()
    return config
  },
  error => {
    // 发送失败
    console.log(error)
    Promise.reject(error)
  }
)

Expected behavior, if applicable

 No cookies in the request

 请求中不携带cookie

Environment:

  • Axios Version:0.18.1
  • OS: window10 1903
  • Browser: Chrome
  • Browser Version: 77.0.3865.120
  • Additional Library Versions :Vue 2.6.10

Additional context/Screenshots

image

@smilledge
Copy link

Axios sends requests using XMLHttpRequest which will always attach cookies (this is not something Axios can control).

And withCredentials only applies to cross site requests (this is documented in more detail here).

The only option I'm aware of is to clear the cookies, then send the request and then set the cookies again. For example;

const tempCookie = document.cookie;
document.cookie = '';
// Send your request
document.cookie = tempCookie;

Although, this may cause issues if you have concurrent requests that require cookies.

@chinesedfan
Copy link
Collaborator

Closed in favor of #2455.

@wangxdmm
Copy link

@qingyuun 如何搞定的?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants