[英]How do I use Here Geocoding and search api from angular service with API key
I'm new to Angular, I want to use Here Geocoding api in my angular service, I appended my apiKey to the httpParams but it didn't work.我是 Angular 的新手,我想在我的 angular 服务中使用 Here Geocoding api,我将我的 apiKey 附加到 httpParams 但它没有用。 Here is my code in the service:
这是我在服务中的代码:
let params = new HttpParams();
params = params.append('at', `${coordinates.latitude},${coordinates.longitude}`);
params = params.append('q', query);
params = params.append('limit', limit);
params = params.append('apiKey', environment.apiMapRest);
return this.httpClient.get('https://discover.search.hereapi.com/v1/discover', {params});
Here is the error:这是错误:
Access to XMLHttpRequest '
from origin 'http://localhost:4200' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
I can access it directly in my browser and postman btw.我可以直接在我的浏览器和 postman btw 中访问它。
This is because from the browser a cross origin call was made.这是因为从浏览器进行了跨源调用。 Here can be found the details about the cross origin policy .
在这里可以找到有关跨源策略的详细信息。 There
hereapi
also documents this case here .这里
hereapi
也记录了这个案例。
I would put the hereapi
request logic on the backend of the Angular application.我会将
hereapi
请求逻辑放在 Angular 应用程序的后端。 This way the Angular app would only communicate with his own backend, and would not do any cross origin calls.这样 Angular 应用程序只会与他自己的后端通信,并且不会进行任何跨源调用。 On the other hand if the
hereapi
calls are on backend side, the apikey is not exposed.另一方面,如果
hereapi
调用在后端,则不会公开 apikey。 (If the hereapi
is called directly from Angular side, the apikey can be taken easily.) (如果直接从
hereapi
端调用hereapi,可以轻松获取apikey。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.