vue axios跨域异常 Access-Control-Allow-Origin wildcard '*' mode is include withCredentials attribute

vue在request时错误信息如下,做此记录:

Access to XMLHttpRequest at 'http://192.168.0.103:8080/user/userLogin' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

Error: ERR_ACTION_ACCESS_UNDEFINED: Are you trying to access this.someMutation() or this.someGetter inside an @Action? 
That works only in dynamic modules. 
If not dynamic use this.context.commit("mutationName", payload) and this.context.getters["getterName"]

原因:得知当请求的凭证模式为'include'时'Access-Control-Allow-Origin' 头的值不能为通配符'*',这里面有一个关键字就是属性值withCredential,原因就是前后端对该项的设置没有统一。

同域安全策略CORS(Cross-Origin Resource Sharing) 
它要求请求的服务器在响应的报头(Response Header)添加 Access-Control-Allow-Origin标签,从而允许此标签所对应域访问此服务器的资源,调用此服务器的接口。

默认情况下请求不提供凭证,通过对withCredential的设置可以控制是否发送凭证。

如果发送的是带凭证的请求,但服务器响应中没有包含这个头部,那么该响应会被浏览器拦截,因此在需要传Cookie等时,服务端的Access-Control-Allow-Origin必须配置具体的域名。

如果服务器接收带凭证的请求会用Access-Control-Allow-Credentials:true的HTTP头部进行响应。

解决:前后端origins配置一致即可,凭证模式通过withCredentials 这个属性控制。个人后端使用的java springboot,先增加对跨域的设置@CrossOrigin(origins = "*", maxAge = 3600),并去掉前端(axios)设置项withCredentials: true。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中解决跨域问题,报错Access-Control-Allow-Origin的方法如下: 1. 在项目的config.js文件中添加以下配置: ```javascript proxy: { '/api': { target: 'http://localhost:8081', // 后端服务器的地址 changeOrigin: true, // 是否跨域 ws: true, // 是否代理 websockets secure: true, // 是否https接口 pathRewrite: { '^/api': '' // 路径重置 } } } ``` 这样配置后,所有以`/api`开头的请求都会被代理到后端服务器。 2. 在Vue文件中编写请求: ```javascript created() { axios.get('api/user/findUserAll').then(function (resp) { console.log(resp) }) } ``` 这样就可以使用axios发送跨域请求了,不再报错Access-Control-Allow-Origin。 需要注意的是,axios本质上是JavaScript的ajax封装,因此会受到同源策略的限制。为了解决跨域问题,我们使用代理方式将请求转发到后端服务器。同时,确保后端服务器已经配置了正确的跨域响应头,例如设置Access-Control-Allow-Origin为允许访问的域名。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Vue3解决跨域问题,报错Access-Control-Allow-Origin](https://blog.csdn.net/weixin_45425813/article/details/123911222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vueaxios实现数据交互与跨域问题](https://download.csdn.net/download/weixin_38745361/14902123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值