axios中cookie跨域及相關(guān)配置示例詳解
前言
最近在跨域、cookie 以及表單上傳這幾個方面遇到了點(diǎn)小問題,做個簡單探究和總結(jié)。本文主要介紹了關(guān)于axios中cookie跨域及相關(guān)配置的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧。
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') // 此處我的項目中使用express框架,跨域使用了cors npm插件
app.use(cors{
credentials: true,
origin: 'http://localhost:8081', // web前端服務(wù)器地址
// origin: '*' // 這樣會出錯
})
成功之后,可在請求中看到
2、我的前端項目代碼的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',我的項目都是用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)
})
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- PHP實(shí)現(xiàn)cookie跨域session共享的方法分析
- 解決前后端分離 vue+springboot 跨域 session+cookie失效問題
- 基于axios 解決跨域cookie丟失的問題
- Angularjs之如何在跨域請求中傳輸Cookie的方法
- 利用nginx解決cookie跨域訪問的方法
- ASP.Net WebAPI與Ajax進(jìn)行跨域數(shù)據(jù)交互時Cookies數(shù)據(jù)的傳遞
- Ajax跨域請求COOKIE無法帶上的完美解決辦法
- Ajax跨域訪問Cookie丟失問題的解決方法
- php跨域cookie共享使用方法
- Cookie跨域問題解決方案代碼示例
相關(guān)文章
用v-html解決Vue.js渲染中html標(biāo)簽不被解析的問題
這篇文章主要給大家介紹了如何利用v-html解決Vue.js渲染過程中html標(biāo)簽不能被解析,html標(biāo)簽顯示為字符串的問題,文中通過圖文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
Vue.js中集成Socket.IO實(shí)現(xiàn)實(shí)時聊天功能
隨著 Web 技術(shù)的發(fā)展,實(shí)時通信成為現(xiàn)代 Web 應(yīng)用的重要組成部分,Socket.IO 是一個流行的庫,支持及時、雙向與基于事件的通信,適用于各種平臺和設(shè)備,本文將介紹如何在 Vue.js 項目中集成 Socket.IO,實(shí)現(xiàn)一個簡單的實(shí)時聊天應(yīng)用,需要的朋友可以參考下2024-12-12
windows下vue-cli及webpack搭建安裝環(huán)境
這篇文章主要介紹了windows下vue-cli及webpack搭建安裝環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

