Angular 6调用localhost API [英] Angular 6 call localhost API

查看:272
本文介绍了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 

任何建议.

推荐答案

您似乎需要

我已经从您的前缀中删除了/*,并添加了pathRewrite部分.我相信您之前尝试代理localhost/myapi/api/site/check-auth(带有额外的/api),应该用pathRewrite部分将其删除.

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.

这篇关于Angular 6调用localhost API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆