admin管理员组文章数量:1419648
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.
My proxy.config.json file:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
package.json file:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
http.service.ts file:
export class HttpService{
static serverApiUrl : string = "/api/"
}
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
any suggestions.
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.
My proxy.config.json file:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
package.json file:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
http.service.ts file:
export class HttpService{
static serverApiUrl : string = "/api/"
}
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
any suggestions.
Share Improve this question edited Dec 28, 2023 at 17:02 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 26, 2018 at 7:31 Mhmd Backer ShehadiMhmd Backer Shehadi 5891 gold badge12 silver badges31 bronze badges 4-
Are you doing
ng serve
ornpm start
? – Amit Chigadani Commented Sep 26, 2018 at 7:40 - npm run serve-dev – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:44
- the app builds but the request return 404 error – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:45
- the real api url is localhost/myapi/site/check-auth – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:46
1 Answer
Reset to default 2It looks like you need to rewrite the URL path, using something like this:
{
"/api": {
"target": "http://localhost",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": "/myapi"
}
}
}
I've removed /*
from the prefix you had and added a pathRewrite
section. I believe what you had before was attempting to proxy to localhost/myapi/api/site/check-auth
(with that extra /api
), which should be stripped with the pathRewrite
section.
本文标签: javascriptAngular 6 call localhost APIStack Overflow
版权声明:本文标题:javascript - Angular 6 call localhost API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745314319a2653078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论