覆盖angular httpclient get请求上的基本URL



我试图对某个URL发出get请求,但它似乎被替换为另一个,这导致了对不存在的URL发出get请求。

这是我的代码

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class ApitestProvider {
tests;
apiUrl = "http//:localhost:8000/api";
testAdded;
testGotten;
testDeleted;
constructor(public http: HttpClient) {
console.log('Hello ApitestProvider Provider');
}
getTests() {
this.http.get(this.apiUrl + "/tests").subscribe(data => {
console.log(data);
this.testGotten = data;
}), err => {
console.log(err);
}
}

但它最终会请求以下URL

url: "http://localhost:8100/http//:localhost:8000/api/tests"

提前谢谢。

您输入的API URL不正确。

而不是这样:apiUrl = 'http//:localhost:8000/api';

应该是这样的:apiUrl= 'http://localhost:8000/api';

Stacklitz

最新更新