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

时间:2017-09-24 17:34:42

标签: javascript api reactjs

我有一个没有使用localhost后端的反应应用。在组件中,有一个onClick处理程序,它调用外部API来获取数据。修改状态以使一个状态成为API的返回值。

该电话的代码:

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

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

在网络详细信息中,我确实获得了200 OK状态代码,这是标题信息: enter image description here

如果您有任何建议,请告诉我。感谢

1 个答案:

答案 0 :(得分:0)

您可以使用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)
})
相关问题