我的項目在後端使用spring,在前端使用angular2。我在spring項目的webapp文件夾下有一個json文件。我試圖從角度訪問它。Angular 2 - 沒有訪問控制允許源標題
我能夠訪問該文件,如果我只需鍵入「http://localhost:8080/project1/test.json」
但是如果我使用的角度相同的鏈接,我得到一個錯誤信息說「沒有訪問控制允許起源頭」
我的角代碼: 1.功能的getJSON()中定義的service.ts:
getJson(){
return this.http.get('http://localhost:8080/project1/test.json')
.map((response:Response) => response.json());
}
調用的getJSON():
results = []; (resJsonData => this.results = resJsonData);這是一個簡單的方法。
我創建proxy.conf.json並添加以下行:
{
"/project1": {
"target": "http://localhost:8080",
"secure": false
}
}
而且還增加了「開始」: 「NG服務--proxy-配置proxy.conf.json」 to package.json
我仍然遇到同樣的問題。我做錯了什麼?
什麼是您的網址看看,當你調用這個頁面是怎樣的? – Chester
@Chester我可否知道你問了哪個網址? –
使用jsonp進行調用,如以下鏈接中所述:https://stackoverflow.com/questions/36289495/how-to-make-a-simple-jsonp-asynchronous-request-in-angular-2/36289568#36289568 – soulzcore