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

config.cancelToken.throwIfRequested is not a function #3875

Closed
max-capricorn opened this issue Jun 30, 2021 · 1 comment
Closed

config.cancelToken.throwIfRequested is not a function #3875

max-capricorn opened this issue Jun 30, 2021 · 1 comment

Comments

@max-capricorn
Copy link

Describe the issue

why like this?
Uncaught (in promise) TypeError: config.cancelToken.throwIfRequested is not a function
at throwIfCancellationRequested (dispatchRequest.js?5270:13)
at dispatchRequest (dispatchRequest.js?5270:24)

Example Code

Code snippet to illustrate your question

export const Http = axios.create({
  baseURL: process.env.VUE_APP_BASE_API || '/',
  withCredentials: true,
  timeout: 20000,
  transformResponse: [
    function (data) {
      return JSONBigString.parse(data);
    }
  ]
});
const CancelToken = axios.CancelToken;
//请求拦截
Http.interceptors.request.use(async (config) => {
  const source = CancelToken.source();
  config.cancelToken = source
  if (!store.getters.token) {
    source.cancel()
  } else {
    let tokenStr = "Bearer " + store.getters.token;
    config.headers.Authorization = tokenStr;
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
  }
  return config
});

Expected behavior, if applicable

this's question to be resolved

Environment

  • Axios Version [0.21.2]
  • Adapter [e.g. XHR/HTTP]
  • Browser [e.g. Chrome, Safari]
  • Browser Version [e.g. 22]
  • Node.js Version [e.g. 13.0.1]
  • OS: [e.g. iOS 12.1.0, OSX 10.13.4]
  • Additional Library Versions [e.g. React 16.7, React Native 0.58.0]

Additional context/Screenshots

Add any other context about the problem here. If applicable, add screenshots to help explain.

@hanischandrew
Copy link

config.cancelToken = source should be config.cancelToken = source.token

Here is a link explaining it a bit further https://masteringjs.io/tutorials/axios/cancel

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

3 participants