请找到下面的网址。
https://stackblitz.com/edit/angular-w4lj6q?file=app%2Fapp.component.ts
我正在使用 http 模块并尝试从 json 文件中获取数据,但我收到以下错误。 你能帮别人解决这个问题吗?
错误:错误.js:55 错误语法错误:JSON 中位置 0 处出现意外的令牌<</p>
堆栈闪电战中出现了一些问题。
- 包含 JSON 的文件夹是apiDta而不是apiData
- 即使使用正确的URL,Stackblitz也不会返回JSON,而是使用HTML进行响应。
您可以使用 reqres 来获取示例 JSON。 更新NameListService
中的网址
private _url: string = "https://reqres.in/api/users";
并更新AppComponent
的ngOnInit()
this._nameService
.getNames()
.subscribe(res => {
this.nameLists = res;
console.log(JSON.stringify(this.nameLists));
});
因为您应该在http
header
中添加等于json
的content-type
确保返回的数据格式json
不像代码那样HTML