【发布时间】:2018-01-23 18:05:41
【问题描述】:
我的项目在后端使用 spring,在前端使用 angular2。我在 spring 项目的 webapp 文件夹下有一个 json 文件。我正在尝试从角度访问它。
只要输入“http://localhost:8080/project1/test.json”就可以访问该文件
但是,如果我使用来自 Angular 的相同链接,我会收到一条错误消息,提示“没有访问控制允许原始标头”
我的角度代码: 1. service.ts中定义的函数getJson():
getJson(){
return this.http.get('http://localhost:8080/project1/test.json')
.map((response:Response) => response.json());
}
-
调用 getJson():
结果=[];
this._manoService.getJson().subscribe(resJsonData => this.results = resJsonData);
我创建了 proxy.conf.json 并添加了以下几行:
{
"/project1": {
"target": "http://localhost:8080",
"secure": false
}
}
并且还在package.json中添加了"start": "ng serve --proxy-config proxy.conf.json",
我仍然遇到同样的问题。我做错什么了吗?
【问题讨论】:
-
当您调用此页面时,您的 URL 是什么样的?
-
@Chester 我可以知道你问的是哪个网址吗?
-
使用 jsonp 进行调用,如下面的链接所述:stackoverflow.com/questions/36289495/…
-
@Mukund 您从中调用网页的 URL。它是否也以
http://localhost:8080开头?还是以file:///开头 -
@Chester 它是“localhost:3000”
标签: java spring angular typescript