我尝试有一个登录按钮,点击call a fetch REST调用。
但是,问题是提取失败,在Javascript控制台中会出现这个错误消息。
//CORS策略阻止了从源提取的访问:请求的资源上不存在'access-control-allow-origin'标头//如果不透明满足您的需要,请将请求模式设置为'no-cors'以在禁用CORS的情况下提取资源。//提取异常失败。
为什么会发生这种情况,为什么我不能从React中的按钮点击调用REST调用?
private handleLogin=()=&>;{console.log("button submit");
// Access to fetch from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
// If an opaque serves your need set the requests mode to 'no-cors' to fetch the resource with CORS disabled.
// Exception Failed to Fetch.
fetch('https://gturnquist-quoters.cfapps.io/api/random')
.then((res) => {
if (res.status >= 400) {
throw new Error("Bad response from server");
}
return res.json();
})
.then((stories) => {
console.log(stories);
});
this.props.login(this.state);
}