這篇文章將為大家詳細(xì)講解有關(guān)axios中cookie跨域及相關(guān)配置的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)專注于建湖企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開發(fā)。建湖網(wǎng)站建設(shè)公司,為建湖等地區(qū)提供建站服務(wù)。全流程按需求定制設(shè)計,專業(yè)設(shè)計,全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
1、 帶cookie請求 - 畫個重點(diǎn)
axios默認(rèn)是發(fā)送請求的時候不會帶上cookie的,需要通過設(shè)置withCredentials: true
來解決。 這個時候需要注意需要后端配合設(shè)置:
header信息 Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin不可以為 '*',因?yàn)?'*' 會和 Access-Control-Allow-Credentials:true 沖突,需配置指定的地址
如果后端設(shè)置 Access-Control-Allow-Origin: '*'
, 會有如下報錯信息
Failed to load http://localhost:8090/category/lists: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8081' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
后端配置缺一不可,否則會出錯,貼上我的后端示例:
const express = require('express') const app = express() const cors = require('cors') // 此處我的項(xiàng)目中使用express框架,跨域使用了cors npm插件 app.use(cors{ credentials: true, origin: 'http://localhost:8081', // web前端服務(wù)器地址 // origin: '*' // 這樣會出錯 })
成功之后,可在請求中看到
2、我的前端項(xiàng)目代碼的axios配置
axios統(tǒng)一配置,會很好的提升效率,避免bug,以及定位出bug所在(方便捕獲到error信息)
建立一個單獨(dú)的fetch.js封裝axios請求并作為方法暴露出來
import axios from 'axios' // 創(chuàng)建axios實(shí)例 const service = axios.create({ baseURL: process.env.BASE_API, // node環(huán)境的不同,對應(yīng)不同的baseURL timeout: 5000, // 請求的超時時間 //設(shè)置默認(rèn)請求頭,使post請求發(fā)送的是formdata格式數(shù)據(jù)// axios的header默認(rèn)的Content-Type好像是'application/json;charset=UTF-8',我的項(xiàng)目都是用json格式傳輸,如果需要更改的話,可以用這種方式修改 // headers: { // "Content-Type": "application/x-www-form-urlencoded" // }, withCredentials: true // 允許攜帶cookie }) // 發(fā)送請求前處理request的數(shù)據(jù) axios.defaults.transformRequest = [function (data) { let newData = '' for (let k in data) { newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&' } return newData }] // request攔截器 service.interceptors.request.use( config => { // 發(fā)送請求之前,要做的業(yè)務(wù) return config }, error => { // 錯誤處理代碼 return Promise.reject(error) } ) // response攔截器 service.interceptors.response.use( response => { // 數(shù)據(jù)響應(yīng)之后,要做的業(yè)務(wù) return response }, error => { return Promise.reject(error) } ) export default service
如下所示,如果需要調(diào)用ajax請求
import fetch from '@/utils/fetch' fetch({ method: 'get', url: '/users/list' }) .then(res => { cosole.log(res) })
關(guān)于“axios中cookie跨域及相關(guān)配置的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
新聞標(biāo)題:axios中cookie跨域及相關(guān)配置的示例分析
轉(zhuǎn)載注明:http://jinyejixie.com/article34/posope.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、微信小程序、App開發(fā)、微信公眾號、動態(tài)網(wǎng)站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)