2016-11-30 70 views
0

我跟着Angular 2英雄教程的T教程,我已經成功地實現了英雄的顯示(我將它們重命名爲員工)。Angular 2,無法從本地主機讀取

但是,本教程使用內存模擬Web服務器。當我試圖通過本地主機網址從我的春天啓動的後端(即顯示JSON),我得到這個錯誤:

XMLHttpRequest cannot load http://localhost:8080/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access 

但是,如果我決定使用在線網絡API,例如:

https://jsonplaceholder.typicode.com/users

然後它工作得很好。

任何幫助將不勝感激。

+0

至少有20個重複的問題。 –

+0

您需要啓用您的API CORS。 –

+0

@RavinderKumar是的,謝謝,這是問題所在,我將它添加爲下面的解決方案。 – Razor88

回答

1

確實有重複的問題(我發佈,因爲他們都沒有解決我的問題)。結果發現其中一人確實讓我走上了正軌。我只是不得不把

@CrossOrigin(origin ="http://localhost:3000") 

在我的控制器類在Spring的頂部。這在春季啓用了CORS。

欲瞭解更多詳情,請看這裏

https://spring.io/blog/2015/06/08/cors-support-in-spring-framework