credentials mode is 'include'
是请求带cookie
function fetchFn(){
let myHeaders = new Headers({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain',
'Cache-Control': 'max-age=10',
});
fetch('http://localhost/getData',{
credentials: 'include',
// headers: myHeaders,
mode:"cors",
// mode:"no-cors",
}).then(function(response){
return response.text()
// response.json().then(res=>console.log(res))
// response.body.then(res=>{console.log(res.text())})
}).then(res=>console.log(res))
}
当请求带cookie但是,后端没有配置corsConfiguration.setAllowCredentials(true);
就会报这个错。
Access to fetch at 'http://10.10.113.186:30333/meta/system/getFriendsLink?systemName=dsd' from origin 'http://127.0.0.1:53746' has been blocked by CORS policy: 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'. fetch.html:1 Uncaught (in promise) TypeError: Failed to fetch
解决办法,两种方法,
- 去掉请求配置
credentials: 'include'
- 添加响应配置
corsConfiguration.setAllowCredentials(true);