Angular 2 app:“XMLHttpRequest无法加载localhost端点”

时间:2017-05-24 21:32:19

标签: rest angular

我将Angular前端设置为尝试命中RESTful端点。 Angular前端在localhost:3000上提供,RESTful后端在localhost:8080上托管。

在我的Angular rest客户端服务中,我进行了调用(我在应用程序的其他地方订阅):

getCurrentSlides(): Observable<Slide[]> {
  return this.http.get("localhost:8080/app/slides")
     .map(extractData)
     .catch(handleError);
}

但是当Angular试图点击该URL时,我收到以下错误:

XMLHttpRequest cannot load localhost:8080/app/slides. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

是的,在我的服务器上启用了CORS。

1 个答案:

答案 0 :(得分:1)

this.http.get("localhost:8080/app/slides")

您错过了网址中的http://。有了这个,大多数浏览器仍然需要CORS用于不同的端口but IE does not,所以在添加http://时你应该能够使用IE进行测试:

  

IE例外

     

当涉及到相同的原始政策时,Internet Explorer有两个主要的例外

     
      
  • 信任区域:如果两个域都位于高度受信任的区域,例如公司域,则不会应用相同的原始限制
  •   
  • 端口:IE并未将端口包含在Same Origin组件中,因此http://company.com:81/index.htmlhttp://company.com/index.html被视为来自同一来源,并且不会应用任何限制。
  •   
     

这些异常是非标准的,并且在任何其他浏览器中都不受支持,但在为Windows RT(或)基于IE的Web应用程序开发应用程序时会有所帮助。

那就是说,你应该启用CORS。 (但似乎你做了,那么它只是缺少的http://前缀。)

相关问题