For my project, I am making an Angular frontend within which numerous HTTP POST requests will be made, with data being passed to the backend and the backend then returning data as a confirmation. I've gotten the first part working, with the backend receiving the data, but as soon as I try to access the data returned I keep finding that the variable I am passing the data to is returning 'Undefined'. I've used Postman to confirm that the backend is receiving and returning data correctly so the frontend is the likely culprit. My code follows as:
Method 1:
onSubmitP1()
{
const formData = new FormData();
formData.append('data_selection', this.SearchDataForm.get('dataselection').value);
console.dir(this.SearchDataForm.get('dataselection').value);
let headerOptions = new HttpHeaders();
headerOptions.append('Content-Type', 'application/text; charset=UTF-8');
return this.httpClient.post(this.SERVER_URL, formData, {headers: headerOptions});
}
Method 2:
onSubmitP2()
{
this.onSubmitP1().subscribe((data) => this.result = data);
console.log(this.result);
console.dir(this.result);
}
Method 2 is called first when my HTML submit button is pressed which then calls Method 1, which actually makes the POST request and returns the observable value. I want the data returned by my web service to be assigned to "result" so I can view it but it keeps returning 'undefined'.