【发布时间】:2020-07-23 15:13:42
【问题描述】:
我对 Angular 比较陌生。我正在尝试从 API 获取数据。调用该 API,我必须将路径变量发送到该 API 以进行搜索。 Angular 是第 7 版。 Angular 项目在 localhost:4200 上运行,而返回 api 的项目在 localhost:8080 上运行。 我有许多其他来自 localhost:8080 的 API 正在运行。他们都工作。除了这个之外,其余的只是没有变量传递的 get 方法。 目前我正在本地设备上进行测试。 API的地址是
http://localhost8080/users/{pathVariable}
我尝试在 Postman 中使用 http://localhost/users/tester 进行测试,它可以工作,返回结果。
然后我尝试在 Angular 中使用该网址:
const url='http://localhost:8080/users/testing';
this.http.get<any>(url).subscribe(data => {this.rowData=data; });
我也尝试过使用变量:
const test='testing';
const url='http://localhost:8080/users/${test}';
this.http.get<any>(url).subscribe(data => {this.rowData=data; });
但它根本不起作用。它总是显示错误,我没有返回任何数据。
我应该如何解决这个错误?
【问题讨论】:
-
这个错误看起来更像是后端的错误。您是否尝试过使用假 api 并检查您是否能够获取数据。使用 Postman 再次检查您的 API
-
是标题和邮递员一样。
-
在使用邮递员进行测试时,我使用了相同的 url 'localhost:8080/users/testing' 并且它有效,我得到了返回数据。我复制了那个网址并尝试了角度,错误发生了。
-
您是否在 Postman 中尝试过
http://localhost/users/testerurl 或http://localhost:8000/users/tester? -
angular 项目在 localhost:4200 上运行,返回 api 的项目在 localhost:8080 上运行。我也试过你说的。它在邮递员中不起作用。