我需要在使用create-react-app实用程序创建的React中使用'CORS'节点模块.
由于它是一个实用程序,我无法调整内部并将'CORS'注入预先配置的'EXPRESS'模块.
我们怎样才能做到这一点?
我正在使用Reactjs并通过javascript中的AJAX使用API。我们如何解决这个问题?以前我使用CORS工具,但现在需要启用CORS。
我在 chrome 上失败了:
访问 'http://******/places?holiday_type=resort¤cy=EUR&checkin=2019-11-01&checkout=2019-11-10&groups[]=1' 来自 origin ' http://localhost:3000 ' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。
我的代码是:
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)
}
Run Code Online (Sandbox Code Playgroud)
}