【发布时间】:2016-11-27 07:02:48
【问题描述】:
我正在尝试使用来自 Google Places API 的照片来增加我在这个项目中为公共艺术作品收集的照片。它说here,您可以发送详细信息请求以获取包含十张照片的数组。最终,我将解包响应,为每张照片获取 Google 的 photo reference,并为数组中的每张照片发出请求并显示它。
很遗憾,当我发送详细信息请求时,这个计划就中断了。这是我得到的确切错误:
Fetch API cannot load https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJ5zf5lfXKRIYR8rPafbwaL68&key=MY_API_KEY.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4040' is therefore not allowed access.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我从本地主机运行它,我很确定这很重要。我所有的其他 API 调用(Google Maps API 和其他几个)都是按原样编写的,所以我有点困惑为什么我会收到错误。以下是相关代码:
The actual API call:
export function getPhotos(placeID) {
let obj = {
method: 'GET'
};
return fetch('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + placeID + '&key=MY_API_KEY')
.then((response) => {
if (response.status >= 400){
throw new Error("Error getting photos for placeID: " + placeID)
}
return response.json()
})
}
如果您需要任何其他信息,请告诉我,我很乐意提供。谢谢!
【问题讨论】:
标签: google-maps-api-3 reactjs cors google-places-api isomorphic-fetch-api