调用外部API时出现CORS/Origin错误
问题描述:
我有一个反应应用程序,没有使用localhost
的后端。在组件中,有一个onClick
处理程序,它调用外部API来获取数据。状态被修改为具有一段状态是API的返回值。调用外部API时出现CORS/Origin错误
该呼叫代码:
fetch(statsURL)
.then(function(response) {
return response.resultSets.rowSet
})
让我知道你是否有任何建议。谢谢
答
您可以使用YQL来请求未与CORS标头一起提供的资源。要获得从fetch()
呼叫使用Response.text()
,Response.json()
,Response.blob()
或Response.arrayBuffer()
fetch(statsURL)
.then(function(response) {
return response.json()
})
.then(function(data) {
console.log(data.resultSets.rowSet)
})
.catch(function(err) {
console.error(err)
})
感谢您的建议的反应。不幸的是,我将这段代码添加到我的文件中,并在控制台中仍然出现相同的错误。 – jsc42
@ jsc42您是否使用YQL来请求资源? – guest271314