withCredentials:默认情况下,跨源请求不提供凭据(cookie、HTTP认证及客户端SSL证明等)。前端通过将withCredentials属性设置为true指定某个请求应该发送凭据
1 2 3 | withCredentials默认值为false。 true:在跨域请求时,会携带用户凭证 false:在跨域请求时,不会携带用户凭证;返回的 response 里也会忽略 cookie |
注意:当配置了 withCredentials = true时,必须后端配置增加 response 头信息Access-Control-Allow-Origin,且必须指定域名,而不能指定为*
如果后端需要带cookie过去,前端需要设置为true
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 服务端需要配置: // 响应头表示是否可以将对请求的响应暴露给页面 Access-Control-Allow-Credentials: true // 允许跨域操作的具体域名 Access-Control-Allow-Origin: "http://localhost:8080" // 允许跨域的HTTP方法 Access-Control-Allow-Methods: ["GET","POST","DELETE"] // 列出将会在正式请求的 Access-Control-Expose-Headers 字段中出现的首部信息 Access-Control-Allow-Headers: ["Content-Type", "Authorization", "Accept"] // 前端需要配置: // 表示跨域请求时是否需要使用凭证 axios.defaults.withCredentials = true |
上一篇:bootstrap插件bootstrap-table-fixed-columns表格列左边右边固定及点击事件
共 0 条评论关于"axios.defaults.withCredentials跨域携带cookie"
最新评论