axios的cookie跨域以及相关配置 | axios no cookie
axios的cookie跨域以及相关配置1、带cookie请求-画个重点axios默认是发送请求的时候不会带上cookie的,需要通过设置withCredentials:true来解决。这个时候需要注意需要后端配合设置:header信息Access-Control-Allow-Credentials:trueAccess-Control-Allow-Origin不可以为*,因为*会和Access-Control-Allow-Credentials:true冲突,需配置指定的地址如果后端设置Access-Control-Allow-Origin:*,会有如下报错信息Failedtoloadhttp://localhost[1]:8090/category/lists:ThevalueoftheAccess-Control-Allow-Originheaderintheresponsemust...
axios的cookie跨域以及相关配置
1、 带cookie请求 - 画个重点
axios默认是发送请求的时候不会带上cookie的,需要通过设置withCredentials: true来解决。 这个时候需要注意需要后端配合设置:
header信息 Access-Control-Allow-Credentials:true Access-Control-Allow-Origin不可以为 *,因为 * 会和 Access-Control-Allow-Credentials:true 冲突,需配置指定的地址如果后端设置 Access-Control-Allow-Origin: *, 会有如下报错信息
Failed to load http://localhost[1]:8090/category/lists: The value of the Access-Control-Allow-Origin header in the response must not be the wildcard * when the requests credentials mode is include. Origin http://localhost[2]: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前端服务器地址 // origin: * //...