我在chrome上失败了:
Access to fetch at 'http://******/places?holiday_type=resort¤cy=EUR&checkin=2019-11-01&checkout=2019-11-10&groups[]=1' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
我的代码是:
getPlaces = async () => {
try {
let response = await fetch(`${BASE_URL}/places?holiday_type=resort¤cy=EUR&checkin=2019-11-01&checkout=2019-11-10&groups[]=1`,
{
method: 'GET',
headers: new Headers({
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
'Access-Control-Request-Method': 'GET, POST, DELETE, PUT, OPTIONS',
'Authorization': 'Basic ' + Base64.encode(apiuser + ':' + apipassword) ,
}),
})
console.log(response)
} catch (error) {
console.log(error)
}
}
最佳答案
CORS通过添加新的HTTP header 来工作,这些 header 允许服务器描述允许使用Web浏览器读取该信息的一组来源。必须在服务器中将其配置为允许跨域。
您可以通过名为CORS的Chrome插件暂时解决此问题。
复制自: How to allow CORS in react.js?
引用: How to overcome the CORS issue in ReactJS?
关于javascript - ReactJS:已被CORS策略阻止:对预检请求的响应未通过访问控制检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57388243/