Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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中使用Json文件时会抛出404错误?_Json_Angular_Get_Http Status Code 404_Httpclient - Fatal编程技术网

在angular中使用Json文件时会抛出404错误?

在angular中使用Json文件时会抛出404错误?,json,angular,get,http-status-code-404,httpclient,Json,Angular,Get,Http Status Code 404,Httpclient,这是我的密码 我试图从本地json文件获取数据,但它返回404错误 有人能帮我解决这个问题吗?请确保您的json文件位于资产文件夹中,然后通过/assets/sample.json请求。非常感谢,现在它可以正常工作了。 import { Component, OnInit } from '@angular/core'; import { Players } from './players' import { HttpClient } from '@angular/common/http' @Co

这是我的密码

我试图从本地json文件获取数据,但它返回404错误


有人能帮我解决这个问题吗?

请确保您的json文件位于
资产
文件夹中,然后通过
/assets/sample.json
请求。非常感谢,现在它可以正常工作了。
import { Component, OnInit } from '@angular/core';
import { Players } from './players'
import { HttpClient } from '@angular/common/http'
@Component({
  selector: 'app-json-with-search',
  templateUrl: './json-with-search.component.html',
  styleUrls: ['./json-with-search.component.scss']
})
export class JsonWithSearchComponent implements OnInit {
  player: Players[];
  constructor( private http:HttpClient) { }

  ngOnInit(): void {
    this.http.get('/sample.json').subscribe((result)=> {
      //this.player = result;
      console.log(result)
    }, (error)=> {
      console.log(error)
    })
  }

}