Angular 中的错误 URL 串联 7
Wrong URL concatenation in Angular 7
当我导航到项目中的不同页面时,控制台显示错误 url 的 404 错误。这是它的样子:
虽然它应该看起来像:
因为我的目标是 example.com
api,所以我在项目中没有指定使用 localhost:4220
api.
这是我的 environment.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 +
,我会得到
Request URL: http://localhost:4220/customers
所以 "localhost:4420/" 部分从哪里来,又在哪里连接?
尝试改变环境。使用 https
协议的文件,如下所示 -
export const environment = {
production: false,
baseUrl: 'https://example.com/',
apiUrl: 'https://example.com/api/'
};
当我导航到项目中的不同页面时,控制台显示错误 url 的 404 错误。这是它的样子:
虽然它应该看起来像:
因为我的目标是 example.com
api,所以我在项目中没有指定使用 localhost:4220
api.
这是我的 environment.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 +
,我会得到
Request URL: http://localhost:4220/customers
所以 "localhost:4420/" 部分从哪里来,又在哪里连接?
尝试改变环境。使用 https
协议的文件,如下所示 -
export const environment = {
production: false,
baseUrl: 'https://example.com/',
apiUrl: 'https://example.com/api/'
};