简体   繁体   English

如何使用此处地理编码和搜索 api 从 angular 服务与 API 密钥

[英]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.

相关问题 在Angle 6中使用地理编码API - Using geocoding API in angular 6 如何获得 Angular Guard 以使用 API 服务进行验证并在失败时重新路由? - How do I get an Angular Guard to use an API service for verifcation and reroute on failure? 如何在 angular 8 - How do I receive data form the API with key and header in angular 8 Angular 9:如何获取从 rest Z8A5DA52ED126057217D3AA9E7AC 服务调用返回的 object 的属性 - Angular 9 : How do I get the properties of an object returned from a rest api service call 如何使用 HERE 地理编码器自动完成 API 与 Angular 和 Jsonp? - How to use HERE Geocoder Autocomplete API with Angular and Jsonp? 我如何在 Angular 应用程序中使用 TawkTo JS API? - How do i Use TawkTo JS API in Angular App? 如何使用Web API中的二进制图像数据作为DOM元素的背景图像(Angular) - How do I use binary image data from Web API as background-image of DOM element (Angular) Angular 13,从 API 接收数据,现在我该如何使用它? - Angular 13, receiving data from API, now how do I use it? 当我从 angular 调用 api 时,如何将 api 键放在后面 - how can I put api key in the back when i call api from angular 从 angular 2 服务设置谷歌地图 API 密钥 - setting google map API key from angular 2 service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM