When working on my Angular app, I often retrieve data from a static JSON file like this:
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
getData() {
this.http.get('/assets/myfile.json').subscribe(data => {
console.log(data);
})
}
}
However, there are situations where myfile.json
may not exist, which is necessary for other purposes. In such cases, I want to avoid getting this error:
HttpErrorResponse : Http failure during parsing for https://localhost:4201/assets/myfile.json
Instead, I simply want to receive a null
response.
Any suggestions on how to achieve this?