【发布时间】:2018-02-20 14:06:25
【问题描述】:
我刚开始使用 Angular 4 和 Spring Boot Web 应用程序。我正在尝试调用 REST 路径:
ngOnInit(): void {
// Make the HTTP request:
this.http.get('/greeting').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
但生成的 URL 是 GET http://localhost:4200/greeting 404 (Not Found)。我研究过这个主题,很多建议是为 NPM 添加一个代理文件,以便更改路由。
"start": "ng serve --proxy-config proxy.conf.json",
还有proxy.conf.json 文件:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
当我运行 npm run start 时,我收到以下消息:
movie-seat@0.0.0 开始
C:\Users\alucardu\Documents\projects\movieseat\frontend\src\main\frontend> ng 服务 --proxy-config proxy.conf.jsonLive Development Server 正在监听 localhost:4200,打开浏览器http://localhost:4200/ **
所以代理文件被加载了但是没有效果。我是在使用过时的信息还是只是做错了什么?
【问题讨论】:
-
我不知道你到底在做什么,但你不能给
get调用一个完整的URL吗? -
现在生成的 URL 是什么?
-
@marouane kadiri 你错了。如果您想调用本地 api,您的服务器可能在另一个端口上运行,因此您需要代理。它还可以让您避免使用完整 URL 时遇到的同源策略问题
-
@Arkej 如问题所述。生成的 url 是
http://localhost:4200/greeting,所以它正在执行来自 NPM 服务器的请求。 -
尝试将您的
proxy.conf.json中的/api更改为/*
标签: angular