我目前有以下 axios 對象 import axios from 'axios';import constants from '../constants.js';import Cookies from 'js-cookie';const API = axios.create({ baseURL: `${constants.urlBackend}`, timeout: 10000, headers: { 'Content-Type': 'application/json' },});API.interceptors.request.use( config => { var accesstoken = Cookies.get('accesstoken'); if (accesstoken) { config.headers.Authorization = `Bearer ${accesstoken}`; } else { delete API.defaults.headers.common.Authorization; } return config; }, error => Promise.reject(error));export default API;例如,我使用如下API.get(constants.urlBackend + "/status/ftp/Server3") .then((response) => { if (response.status === 200) { this.setState({ sonServerThreeStatus: true }); } else { this.setState({ sonServerThreeStatus: false }); } }) .catch((error) => { this.setState({ sonServerThreeStatus: false }) });重點是,到目前為止我使用該對象使用的所有請求都有 application/json 內容標頭,但我面臨一個問題,其中有些需要headers: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' }所以我不知道如何在各種標頭之間觸發,或者如何使該對象在多個選項之間進行選擇,以及我將如何調用它。
如何修改這個 axios 對象,以便我可以在各種標題之間進行選擇?
慕的地8271018
2022-05-26 16:33:44