Angular http get call failling



请找到下面的网址。

https://stackblitz.com/edit/angular-w4lj6q?file=app%2Fapp.component.ts

我正在使用 http 模块并尝试从 json 文件中获取数据,但我收到以下错误。 你能帮别人解决这个问题吗?

错误:错误.js:55 错误语法错误:JSON 中位置 0 处出现意外的令牌<</p>

堆栈闪电战中出现了一些问题。

  1. 包含 JSON 的文件夹是apiDta而不是apiData
  2. 即使使用正确的URL,Stackblitz也不会返回JSON,而是使用HTML进行响应。

您可以使用 reqres 来获取示例 JSON。 更新NameListService中的网址

private _url: string = "https://reqres.in/api/users";

并更新AppComponentngOnInit()

this._nameService
.getNames()
.subscribe(res => { 
this.nameLists = res;
console.log(JSON.stringify(this.nameLists));
});

因为您应该在httpheader中添加等于jsoncontent-type确保返回的数据格式json不像代码那样HTML

最新更新