亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修改這個 axios 對象,以便我可以在各種標題之間進行選擇?

如何修改這個 axios 對象,以便我可以在各種標題之間進行選擇?

慕的地8271018 2022-05-26 16:33:44
我目前有以下 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'             }所以我不知道如何在各種標頭之間觸發,或者如何使該對象在多個選項之間進行選擇,以及我將如何調用它。
查看完整描述

1 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

解決了,我從對象中刪除了標題,現在我只需在啟動對象時指定它們

 API.post(constants.urlBackend + "/register", qs.stringify({ email, password }), {
             'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        }).


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號