I’m new to combining Angular with Django and I’m struggling with API requests. I’ve set up a Django backend using rest_framework and an Angular 7 frontend. The issue is that my Angular app is trying to fetch data from its own server (localhost:4200) instead of the Django server (localhost:8000).
Here’s a simplified version of what I’m attempting:
export class DataComponent implements OnInit {
info: any;
isLoaded: boolean = false;
hasIssue: boolean = false;
constructor(private httpClient: HttpClient) { }
ngOnInit() {
this.httpClient.get('/api/data').subscribe(
response => {
this.info = response;
console.log(response);
},
err => {
this.isLoaded = true;
this.hasIssue = true;
console.log(err);
}
);
}
}
I know I could create a service with a baseUrl variable, but is there a more elegant way to automatically direct all API requests to the backend server? Any guidance or best practices for this kind of setup would be really appreciated!