【发布时间】:2019-07-31 03:59:34
【问题描述】:
我正在尝试通过在 Angular 中创建的 Web 应用程序运行我在 Azure 中拥有的索引器。我找到了文档here。但是,每当我尝试根据此文档运行索引器时,都会被 CORS 阻止。这是在我的 service.ts 中调用索引器的代码
public async runIndex(){
const httpHeaders = new HttpHeaders({
'api-key': "api-key's value here",
'Accept': 'application/json'
'content-type': 'application/json'});
let params = new URLSearchParams();
params.set('api-version', this.apiVersion);
await this.speechHttp.post(this.indexUrl, params, {headers: httpHeaders}).subscribe(
error => this.errorMessage = <any>error
);
这没有用,所以我做了更多的挖掘,在我阅读的文章中,我发现this one 指出需要从端口 443 进行 API 调用。所以我从 Angular 的默认端口 4200 更改为端口 443,但是那仍然行不通。我试过创建一个代理文件,然后运行ng serve --proxy-config proxy.conf.json,但这也没有用。有没有办法在 Angular 中解决这个问题?上次我创建的 API 出现 CORS 问题时,我不得不解决服务器/API 端的问题,但我无法访问 Azure Indexer 的 API。
这是 CORS 错误消息。
从源“http://localhost:443”对“https://my-indexer.search.windows.net/indexers/my-indexer/run?api-version=2019-05-06”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。
看起来 API 正在阻止请求,即使请求的来源是建议的端口 443。
【问题讨论】: