vue axios 配置上服务器后报错'Access-Control-Allow-Origin' header]

ue项目配置到服务器后,请求能够成功,返回的数据也能在浏览器中看见,但是报错:

Failed to load http://pre.api.jmxy.mockuai.c...: 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://pre.promotion.jmxy.moc...' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

clipboard.png

请求状态码为200. 在浏览器中也能看到返回的数据:

我配置的axios为:

 
var instance = axios.create({
  baseURL: baseUrl,
  timeout: 1000 * 12, // 创建axios实例,设定超时时间是12s
  withCredentials: true, // 允许携带cookie
  headers: {
    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
  }
});

解决方法:

withCredentials: false, // 允许携带cookie

在axios里把这一条设置为false就行了,我们的项目是不需要携带cookie的,所以之前的项目后端都没有改过,我刚接手,所以不太清楚,才闹出这个问题,如果需要携带cookie的话,我搜了一圈下来,基本上都和下面几位的回答的是一样的,需要后端配置。

 
原文地址:https://www.cnblogs.com/braveLN/p/11424710.html