【发布时间】:2018-12-09 05:03:54
【问题描述】:
我正在尝试通过一个简单的 React 应用程序访问 AWS API,但遇到了 CORS 错误。我要做的就是访问数据,然后显示它并允许用户组织它。当我将数据的 url 插入浏览器时,信息显示完美。
我的 API 文件如下所示:
const beerURL = "https://s3.amazonaws.com/....";
//Grab all the data from the API so we can use it in our application
export default {
searchBeer: () => {
return fetch(beerURL)
}
}
在我的 App.js 文件中,我这样调用:
import API from "./utils/API";
class App extends Component {
constructor(props) {
super(props);
this.state = {
beer: []
}
}
componentDidMount() {
this.loadData();
}
loadData = () => {
Promise.all([
API.searchBeer()
])
.then(response => {
this.setState({ beer: response });
})
}
render() {
...
}
我需要为此设置 CORS 服务器吗?我不明白为什么我需要为一个简单的 GET 请求这样做。
【问题讨论】:
-
请尝试禁用 chrome 网络安全。当您尝试访问跨域网址时。
-
@RinkeshGolwala,我在另一个浏览器中尝试过,但仍然遇到同样的问题
-
是的。也是出于同样的原因。
-
这可能有助于moxio.com/blog/12/…
-
你能展示你的 API.js 吗?
标签: reactjs api amazon-web-services cors