企业电子商务网站平台建设,奉贤做网站公司,重庆h5制作,佰联轴承网做的网站场景#xff1a;
常见的跨域报错#xff0c;一般都是由后端进行setHeader/*什么的。但是现在这种情况就是后端说他们做了处理。但是我这边请求还是报错。 withCredentials:
with-credentials用来设置是否发送cookie#xff0c;如果为true就会在跨域请求时候携带cookie
常见的跨域报错一般都是由后端进行setHeader/*什么的。但是现在这种情况就是后端说他们做了处理。但是我这边请求还是报错。 withCredentials:
with-credentials用来设置是否发送cookie如果为true就会在跨域请求时候携带cookie如果是同一个域名则设置什么都无妨但是如果不是同一个域名下/外部接口携带了cookie的话后端就不能将Access-Control-Allow-Origin设置为*。因为设置为*时cookie不会出现在http的请求头里。
修改方法:
// 创建axios实例const service axios.create({baseURL: process.env.VUE_APP_URL, // 测试环境withCredentials: true,timeout: 20000 // 请求超时时间
})// request拦截器
service.interceptors.request.use(config {如果是外部的请求则设置为falseif (config.url config.url.indexOf(/data/board/distributed) 0){ config.withCredentials false}config.headers[Content-Type] application/json; charsetUTF-8if (JSON.parse(localStorage.getItem(user))) {config.headers[Authorization] dowsure JSON.parse(localStorage.getItem(user)).token;}return config
}, error {// Do something with request errorPromise.reject(error)
})或者(我没有试过)
将Content-Type: application/json改为Content-Type: application/x-www-form-urlencoded