Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular http获取呼叫失败_Angular - Fatal编程技术网

Angular http获取呼叫失败

Angular http获取呼叫失败,angular,Angular,请找到下面的url 我正在使用http模块,并试图从json文件中获取数据,但我得到以下错误信息。你能帮别人解决这个问题吗 错误:errors.js:55 Error SyntaxError:Unexpected token

请找到下面的url

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


错误:errors.js:55 Error SyntaxError:Unexpected token<在JSON中的位置0

,因为您应该在
http
头中添加
内容类型
等于
JSON

为了确保返回的数据格式是
json
而不是
HTML
,正如您的代码所做的那样

,stackblitz中出现了一些问题

  • 包含JSON的文件夹是apiDta,而不是apiData
  • Stackblitz即使返回正确的URL也不会返回JSON,而是使用HTML进行响应
  • 您可以使用获取示例JSON。 更新
    NameListService

    private _url: string = "https://reqres.in/api/users";
    
    并更新AppComponent的
    ngOnInit()


    我可以证实,这是有效的。在reqres中也有不错的发现:)
    this._nameService
        .getNames()
        .subscribe(res => { 
                              this.nameLists = res;
                              console.log(JSON.stringify(this.nameLists));
                          });