首页 > 解决方案 > Angular 7 中的错误 URL 连接

问题描述

当我导航到项目中的不同页面时,控制台显示 404 错误和错误的 url。这是它的样子:

https://localhost:4420/example.com/api/customers

虽然它应该看起来像:

https://example.com/api/customers

当我以example.comapi 为目标时,我在项目中没有指定使用localhost:4220api。

这是我的环境.ts:

export const environment = {
  production: false,
  baseUrl: 'example.com/',
  apiUrl: 'example.com/api/'
};

以下是我使用该服务的方式:

export class CustomersComponent implements OnInit  {
  customers: any;

  constructor(private http: HttpClient) { }

  ngOnInit() {
    const token = localStorage.getItem('jwt');
    this.http.get(environment.apiUrl + 'customers', {
      headers: new HttpHeaders({
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json'
      })
    }).subscribe(response => {
      this.customers = response;
    }, err => {
      console.log(err);
    });
  }
}

如果我environment.apiUrl +从上面的代码中删除,我会得到

请求网址:http://localhost:4220/customers

那么“localhost:4420 /”部分来自哪里以及它在哪里连接?

标签: angulartypescripturl-routing

解决方案


尝试更改环境。带有https协议的文件,如下所示 -

export const environment = {
  production: false,
  baseUrl: 'https://example.com/',
  apiUrl: 'https://example.com/api/'
};

推荐阅读