我尝试在我的angular 6应用程序中本地调用API,API URL是http://localhost/myapi
。我添加了一个指向此URL的代理配置文件,但在运行我的应用程序时,我收到了404
错误。
我的proxy.config.json文件:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
字符串
package.json文件:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
型
http.服务.ts文件:
export class HttpService{
static serverApiUrl : string = "/api/"
}
型
验证服务ts文件:
this.http.get(HttpService.serverApiUrl+"site/check-auth")
.map(response => {
if(response['auth'] == 1) {
return true;
} else {
this.router.navigate(['/auth'])
return false;
}
});
型
在控制台里我得到了这个:
http://localhost:4200/api/site/check-auth
型
有任何建议。
1条答案
按热度按时间but5z9lq1#
看起来你需要重写URL路径,使用类似这样的东西:
字符串
我已经从你的前缀中删除了
/*
,并添加了一个pathRewrite
部分。我相信你之前试图代理到localhost/myapi/api/site/check-auth
(使用额外的/api
),这应该与pathRewrite
部分一起剥离。