简体   繁体   English

使用 Angular 7 在 Azure Maps 帐户中启用 CORS

[英]Enable CORS in Azure Maps Account with Angular 7

I am using now Azure Maps Account with Angular 7 and try to create an Autocomplete input.我现在正在使用带有 Angular 7 的 Azure Maps 帐户并尝试创建一个自动完成输入。

I am calling the URL:我正在调用 URL:

this.http.get('https://atlas.microsoft.com/search/address/json?subscription-key=<key>&api-version=1.0&query=Newyork').subscribe(...)

When I run this link in postman, it works perfectly and responses with the data that I want.当我在邮递员中运行此链接时,它可以完美运行并响应我想要的数据。 But when I call in in Angular using HttpClient, I am getting a CORS problem.但是当我使用 HttpClient 在 Angular 中调用时,我遇到了 CORS 问题。

I cannot find a place where to change the cors in the service.我找不到可以在服务中更改 cors 的地方。 Does anybody know how to fix it?有人知道如何解决吗?

Besides, if someone copied this URL and used it in somewhere else, he will use the service but on my cost.此外,如果有人复制了这个 URL 并在其他地方使用了它,他将使用该服务,但需要支付我的费用。 How can I secure it with specific Urls?如何使用特定的 URL 来保护它?

CORS (Cross-Origin Resource Sharing) is a way for the server to say "I will accept your request, even though you came from a different origin." CORS(跨域资源共享)是服务器表示“即使您来自不同的来源,我也会接受您的请求”的一种方式。 This requires cooperation from the server – so if you can't modify the server (eg if you're using an external API), this approach won't work.这需要服务器的合作——因此如果您无法修改服务器(例如,如果您使用外部 API),则此方法将不起作用。

Modify the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere (or specify a domain instead of *).修改服务器以添加头 Access-Control-Allow-Origin: * 以启用来自任何地方的跨域请求(或指定域而不是 *)。

Alternatively the plugins worked with HTTP but not with the latest httpClient.或者,这些插件适用于 HTTP,但不适用于最新的 httpClient。 Also, configuring the CORS response headers on the server wasn't really an option.此外,在服务器上配置 CORS 响应标头并不是一个真正的选择。 So, I created a proxy.conf.json file to act as a proxy server.因此,我创建了一个 proxy.conf.json 文件来充当代理服务器。

proxy.conf.json file: proxy.conf.json文件:

"/posts": {
    "target": "https://example.com",
    "secure": true,
    "pathRewrite": {
    "^/posts": ""
  },
    "changeOrigin": true
  }
}

I placed the proxy.conf.json file right next the the package.json file in the same directory.我将proxy.conf.json文件放在同一目录中的package.json文件旁边。

Then I modified the start command in the package.json file:然后我修改了package.json文件中的start命令:

"start": "ng serve --proxy-config proxy.conf.json"

The HTTP call from my app component:来自我的应用程序组件的 HTTP 调用:

return this._http.get('/posts/pictures?method=GetPictures')
.subscribe((returnedStuff) => {
  console.log(returnedStuff);
});

Lastly to run my app, I'd have to use npm start or ng serve --proxy-config proxy.conf.json最后要运行我的应用程序,我必须使用npm start or ng serve --proxy-config proxy.conf.json

You can read more here .您可以在此处阅读更多内容。

Hope it helps.希望能帮助到你。

Azure Maps has now added CORS policy which is less restrictive. Azure Maps 现在添加了限制较少的 CORS 策略。 All response headers should now contain the necessary 'Access-Control-Allow-Origin': '*' .所有响应头现在都应该包含必要的'Access-Control-Allow-Origin': '*' Also followed up on this thread here .还在这里跟进了这个线程。

Azure REST APIs respond with CORS response headers, but uses 'Access-Control-Allow-Origin': '*' as response header. Azure REST API 使用 CORS 响应标头进行响应,但使用'Access-Control-Allow-Origin': '*'作为响应标头。 While using fetch to call API credentials: include is sent by default, browser complains about below使用fetch调用 API credentials: include默认发送credentials: include ,浏览器报错如下

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.当请求的凭据模式为“包含”时,响应中“Access-Control-Allow-Origin”标头的值不得为通配符“*”。

fetch API provides options to send credentials mode. fetch API提供了发送credentials模式的选项。 Using this calling Azure API works使用此调用 Azure API 有效

fetch('https://example.com', {
  credentials: 'omit' // or 'same-origin'
})

Other option would be to proxy to this API via gateways/service layer etc.其他选项是通过网关/服务层等代理到这个 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM