【问题标题】:Cors error with Angular4 http request public rest apiAngular4 http请求公共休息api的Cors错误
【发布时间】:2026-01-09 14:00:01
【问题描述】:

Ĥello,我有点困惑,我不知道如何解决 cors 的问题。这绝对不是服务器的问题,因为我在我的应用程序之外的每个 rest api 都得到它。

我正在使用以下 rest api 进行测试

[http://www.groupkt.com/post/f2129b88/free-restful-web-services-to-consume-and-test.htm][1]

代码:

async getData(){
  const url = "http://www.groupkt.com/post/c9b0ccb9/country-and-other-related-rest-webservices.htm";
  const response = await this.http.get(url).toPromise();
  console.log(response)
}
}

HTML

<button (click)="getData();"></button>

我收到以下错误:

XMLHttpRequest 无法加载 http://www.groupkt.com/post/c9b0ccb9/country-and-other-related-rest-webservices.htm。 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 使用权。 [1]: http://www.groupkt.com/post/f2129b88/free-restful-web-services-to-consume-and-test.htm

【问题讨论】:

    标签: angular typescript cors ember-cli angular-cli


    【解决方案1】:

    试试这个Url http://services.groupkt.com/country/get/all(例如),这是一个WebService示例。您在代码中使用的 url 只是服务的描述。

    【讨论】: