我使用了 Axios 攔截器來攔截我的 Axios 登錄請求。但它也被其他 axios 請求使用。擁有攔截器是否意味著所有 axios 請求都會使用它?如果是,我如何僅將其用于特定請求。在這種情況下,我的登錄請求? Axios.interceptors.response.use(res => { // console.log(`complete response ---> ${JSON.stringify(res)}`) // console.log(`This is the login response----> ${JSON.stringify(res.headers)}`) const authorization = res.headers.authorization; console.log(`Entering login`); const bearerToken = authorization.substring(7, authorization.length); const userLoginResponse: LoginResponse = { httpStatus: res.status, token: bearerToken, user: { . . . } } this.authParams = { . . . . } //console.log(`This is the userLoginResponse ---->${JSON.stringify(userLoginResponse)}`) this.setObject(); resolve(userLoginResponse) return res; }, (error) => { console.log(`This is the error status ---> ${error.response.status}`) if (error.response.status === 401) { resolve(error.response); } }) await Axios.post(loginAPIURL, params, config);這是另一個 axios 請求: const submitAPIURL = process.env.REACT_APP_API_ADD_INITIATIVE_URL || ""; axios.post(submitAPIURL, initiative, config).then(submitRes =>{ resolve(submitRes.headers) }).catch(error => { reject(error); }) });但是上述請求也調用了登錄axios請求的axios攔截器。
axios攔截器攔截所有axios請求
MMMHUHU
2023-07-06 15:30:29