I'm currently developing a Web Application using Angular and need to send an HttpRequest with a get method to fetch data from my api server.
Below is the code snippet for my http get data client:
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
getData(url: string): Observable<any> {
console.log("getting data from API with url: " + url);
return this.http.get(url, { responseType: 'json' });
}
}
To call this function on a button click, I use the following code:
this.dataService.getData(requestString).subscribe(
(data: any) => {
console.log(data);
});
However, when testing it out, I encounter the error message:
ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: 'Unknown Error', url: '...', ok: false, …}
Interestingly, when I try the same URL in Postman, it works perfectly fine. Any idea what might be missing or incorrect in my setup?