angularjs Angular 6调用localhost API

ktecyv1j  于 6个月前  发布在  Angular
关注(0)|答案(1)|浏览(74)

我尝试在我的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


有任何建议。

but5z9lq

but5z9lq1#

看起来你需要重写URL路径,使用类似这样的东西:

{
    "/api": {
        "target": "http://localhost",
        "secure": false,
        "logLevel": "debug",
        "pathRewrite": {
            "^/api": "/myapi"
        }
    }
}

字符串
我已经从你的前缀中删除了/*,并添加了一个pathRewrite部分。我相信你之前试图代理到localhost/myapi/api/site/check-auth(使用额外的/api),这应该与pathRewrite部分一起剥离。

相关问题