fetch ajax cros,React fetch有错误fetch被CORS策略阻止[重复]

我尝试有一个登录按钮,点击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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值