使用axios設置headers

一、axios設置header跨域

在前後端分離的開發中,前端常常需要通過ajax請求獲取後端數據,而由於前後端分離的應用一般會使用不同的域名或端口,所以需要解決跨域問題。使用axios發送請求時,需要在headers中設置一些參數來解決跨域問題。

在axios的config對象中設置參數就可以解決跨域問題,以下是一個設置跨域header的示例:


axios.defaults.baseURL = 'http://localhost:3000'; //設置默認url
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //設置post請求的header
axios.defaults.withCredentials = true; //設置跨域攜帶cookies
axios({
    url: '/api/user',
    method: 'post',
    headers: {
        'Access-Control-Allow-Origin': '*'
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

二、axios設置header解決跨域

在使用axios設置header時,有可能會遇到一些跨域問題。在此情況下,可以在請求頭中設置Origin和Referer來解決跨域問題。


axios({
    url: '/api/user',
    method: 'post',
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Origin':'http://www.example.com',
        'Referer': 'http://www.example.com/user'
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

三、axios設置header無效

有時候在設置axios的header時會遇到一些無效的問題,這時候需要注意以下幾點:

  • 檢查請求路徑是否正確
  • 檢查請求方法是否正確
  • 檢查請求頭是否正確
  • 檢查發送的數據是否符合要求
  • 檢查服務器是否正確處理請求

下面是一個設置無效header的示例:


axios({
    url: '/api/user',
    method: 'post',
    headers: {
        'Content-Type': 'application/json'
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

四、axios設置header不生效

有時候在設置axios的header時會遇到一些不生效的問題,這時候需要檢查以下幾點:

  • 是否在全局配置中設置了相同的header,導致覆蓋了當前請求的header
  • 是否允許當前域請求對應的資源
  • 是否遵循同源策略限制
  • 是否發送的請求數據的編碼格式不正確

以下是一個設置不生效header的示例:


axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
axios({
    url: '/api/user',
    method: 'get',
    headers: {
        'Authorization': token
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

五、axios設置請求超時時間

在axios中,可以設置請求超時時間,當請求超時時,會自動取消請求,避免無效請求的佔用資源。


axios({
    url: '/api/user',
    method: 'get',
    timeout: 5000, //設置超時時間為5秒
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

六、axios設置請求頭header

在axios中,可以自定義請求頭header,以便於在接口中獲取相關信息。


axios({
    url: '/api/user',
    method: 'post',
    headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json'
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

七、axios設置cookie

在axios請求時,可以設置攜帶cookie,讓服務器可以獲取到前端的cookie值以判定身份信息。


axios({
    url: '/api/user',
    method: 'post',
    withCredentials:true, //設置是否攜帶cookie,默認false
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

八、完整示例代碼


axios.defaults.baseURL = 'http://localhost:3000'; //設置默認url
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //設置post請求的header
axios.defaults.withCredentials = true; //設置跨域攜帶cookies

axios({
    url: '/api/user',
    method: 'post',
    headers: {
        'Access-Control-Allow-Origin': '*'
    },
    data: {
        name: 'tony',
        job: 'engineer'
    }
}).then(function(response){
    console.log(response.data);
}).catch(function(error){
    console.log(error);
});

以上就是使用axios設置headers的詳細介紹,希望對大家有所幫助!

原創文章,作者:JYMD,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/145113.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JYMD的頭像JYMD
上一篇 2024-10-26 11:55
下一篇 2024-10-26 11:55

相關推薦

  • 使用axios獲取返回圖片

    使用axios獲取返回圖片是Web開發中很常見的需求。本文將介紹如何使用axios獲取返回圖片,並從多個方面進行詳細闡述。 一、安裝axios 使用axios獲取返回圖片前,首先需…

    編程 2025-04-29
  • Axios請求數據亂碼問題解決

    本文將從以下三個方面詳細闡述Axios請求數據亂碼問題的原因和解決方法: 一、設置請求頭 Axios請求數據亂碼的原因可能是因為請求時沒有設置請求頭的編碼方式,而且默認的編碼方式是…

    編程 2025-04-28
  • Python爬蟲中Headers的應用

    Headers是在進行爬蟲過程中最為重要的部分之一,其在實現網絡爬取數據時起到了至關重要的作用。本文將從多個方面介紹Python爬蟲中Headers的應用。 一、Headers的概…

    編程 2025-04-27
  • axios body傳參詳解

    在前端開發中,我們經常與後台進行數據交互,其中一個很重要的環節就是數據傳參。而axios是目前非常優秀的一款前端HTTP請求庫,它可以輕鬆實現不同方式的數據傳參,本文將以axios…

    編程 2025-04-25
  • Axios的優點:為什麼它是當前最受歡迎的HTTP請求庫

    從2014年Axios隨着Vue.js的發布而出現以來,Axios已經成為了以 Node.js 為平台的一個廣泛應用的輕量級的 HTTP 請求客戶端。雖然還有其他的HTTP請求庫,…

    編程 2025-04-24
  • axios面試題詳解

    一、axios簡介 axios是一個基於Promise的HTTP客戶端,可以用於瀏覽器和Node.js環境中發送請求。它具有以下幾個特點: 1、支持瀏覽器和Node.js //ax…

    編程 2025-04-23
  • axios.put詳解

    一、基本介紹 axios是一個基於Promise的HTTP客戶端,可用於瀏覽器和node.js。 axios的特點: 從瀏覽器中創建XMLHttpRequests 從node.js…

    編程 2025-04-23
  • Vue中使用axios的完整指南

    一、axios在vue中的使用慕課筆記 Axios 是一個基於 Promise 的 HTTP 庫,可用於瀏覽器和 Node.js,它是一種簡單,易用且兼容性好的發起HTTP請求的方…

    編程 2025-04-20
  • 詳解Axios Patch方法

    Axios是一個基於Promise的HTTP客戶端,可以在瀏覽器和Node.js中使用。它提供了一個易於使用的API,可以處理HTTP請求和響應。Axios支持所有現代瀏覽器,包括…

    編程 2025-04-20
  • 深入淺出axios跨域

    Axios是一個基於Promise的HTTP客戶端,用於創建RESTful API請求。在Web開發中,跨域是不可避免的,而Axios正是開發中最好的解決方案之一。在本篇文章中,我…

    編程 2025-04-13

發表回復

登錄後才能評論