我如何在此处使用地理编码并使用API 密钥从角度服务中搜索api

问题描述 投票:0回答:1

我是Angular的新手,我想在我的angular服务中使用Here Geocoding api,我将apiKey附加到了httpParams上,但是没有用。这是我在服务中的代码:

    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});

这里是错误:

Access to XMLHttpRequest at 'https://discover.search.hereapi.com/v1/discover?at=21.028511,105.804817&q=ca&limit=5&apiKey=81hBUb9HvwHCPKRBRyG4ciE-QbkEcAoEsuV8rFa3Zoc'
from origin 'http://localhost:4200' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

我可以直接在浏览器和邮递员中访问它。

angular here-api
1个回答
0
投票

这是因为从浏览器中进行了跨源调用。在这里可以找到有关cross origin policy的详细信息。还有hereapi文档this case here

我将hereapi请求逻辑放在Angular应用程序的后端。这样,Angular应用程序将仅与自己的后端通信,而不会进行任何跨源调用。另一方面,如果hereapi调用位于后端,则不会公开apikey。 (如果直接从Angular一侧调用hereapi,则可以轻松使用apikey。)

© www.soinside.com 2019 - 2024. All rights reserved.