I'm trying to send data to my asp.net server with angular. The data at the fron-end part is okay and i have tested it. But for some reason when i try to send these data to my server with a post request, angular sends null data to my server. I tried sending data to my server from postman and it works without any problem and my server send as response true as excepted. I don't know what i'm missing inside my front-end...
Here is my angular code with the post request:
// Set admin ID to object and create new school
schoolData.AdminId = 0;
const schoolHeaders = { 'Content-Type': 'application/json' };
const schoolBody = schoolData
this.http.post<any>(this.baseUrl "Api/CreateSchool", { body: JSON.stringify(schoolBody) }, { headers: schoolHeaders }).subscribe(schoolResponse => {
if (schoolResponse) {
this.router.navigate(['/home']);
}
}, error => console.error(error));
My asp.net code:
[HttpPost]
[Route("Api/CreateSchool")]
public bool CreateNewSchool([FromBody] SchoolModel schoolData)
{
// IT PRINTS THE NAME ONLY FROM POSTMAN NOT IF THE REQUEST IS FROM ANGULAR
Console.WriteLine("Name is: " schoolData.Name);
SchoolManager context = HttpContext.RequestServices.GetService(typeof(SchoolManager)) as SchoolManager;
return context.CreateNewSchool(schoolData);
}
CodePudding user response:
I had to remove the {}
from { body: JSON.stringify(schoolBody) }