调用外部API时出现CORS/Origin错误

问题描述:

我有一个反应应用程序,没有使用localhost的后端。在组件中,有一个onClick处理程序,它调用外部API来获取数据。状态被修改为具有一段状态是API的返回值。调用外部API时出现CORS/Origin错误

该呼叫代码:

fetch(statsURL) 
    .then(function(response) { 
     return response.resultSets.rowSet 
    }) 

这会产生以下错误: enter image description here

在网络细节,我得到一个200 OK状态代码,而这里头信息: enter image description here

让我知道你是否有任何建议。谢谢

您可以使用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) 
}) 
+0

感谢您的建议的反应。不幸的是,我将这段代码添加到我的文件中,并在控制台中仍然出现相同的错误。 – jsc42

+0

@ jsc42您是否使用YQL来请求资源? – guest271314