Angular 6调用localhost API [英] Angular 6 call localhost API
本文介绍了Angular 6调用localhost API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的angular 6应用程序中本地调用api,api的URL为http://localhost/myapi
我添加了一个指向该URL的代理配置文件,但是
运行我的应用程序时出现404
错误.
I am trying to call an api locally in my angular 6 app, the api url is http://localhost/myapi
I added a proxy config file that point to this url but I got
404
error when I run my app.
我的proxy.config.json文件:
My proxy.config.json file:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
package.json文件:
package.json file:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
http.service.ts文件:
http.service.ts file:
export class HttpService{
static serverApiUrl : string = "/api/"
}
auth.service.ts文件:
auth.service.ts file :
this.http.get(HttpService.serverApiUrl+"site/check-auth")
.map(response => {
if(response['auth'] == 1) {
return true;
} else {
this.router.navigate(['/auth'])
return false;
}
});
在控制台中,我得到了:
In the console I got this:
http://localhost:4200/api/site/check-auth
任何建议.
推荐答案
查看全文