javascript - Angular 6 call localhost API - Stack Overflow

I am trying to call an API locally in my angular 6 app, the API URL is http:localhostmyapi I added a

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 or npm 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
Add a ment  | 

1 Answer 1

Reset to default 2

It 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314319a4622143.html

相关推荐

  • javascript - Angular 6 call localhost API - Stack Overflow

    I am trying to call an API locally in my angular 6 app, the API URL is http:localhostmyapi I added a

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信