我尝试使用Angular 5从Google Places API请求一些信息,但我总是收到此CORS错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
但是,当我从开发人员工具中看到网络选项卡时,我可以看到http调用正在返回包含所请求信息的200状态代码。
这就是我调用Places API的方式:
getAutocompleteSuggestions(location: string, position?: Position): Observable<Object> {
let params = new HttpParams()
.append('input', location)
.append('language', navigator.language)
.append('key', environment.googlePlacesApiKey);
if (position) {
params = params.append('location', `${position.coords.latitude},${position.coords.longitude}`);
}
return this.http.get(GOOGLE_API_URL, { params });
}
这是在开发人员工具的网络选项卡中看到的调用:
有关如何避免这种情况的任何想法吗?