I'm using the React template when creating a new project in Visual Studio. It has something like this for GET request:
fetch('api/SampleData/WeatherForecasts')
.then(response => response.json())
.then(data => {
this.setState({ forecasts: data, loading: false });
});
I'm learning how to do a POST request, so I modified the code to:
const formData = new FormData();
formData.append('values', this.state.values);
fetch('api/SampleData/WeatherForecasts', {
method: 'POST',
body: formData
}).then(response => response.json())
But not sure how to retrieve the formData on ASP.Net:
[HttpPost("[action]")]
public string WeatherForecasts()
{
// How to print out the values from formData here
// System.Diagnostics.Debug.WriteLine(Request.Form.ToString());
return "Hello";
}
Edit: I also don't know how to return a Json result from ASP.Net:
[HttpPost("[action]")]
public JsonResult WeatherForecasts()
{
// How to print out the values from formData here
// System.Diagnostics.Debug.WriteLine(Request.Form.ToString());
// How to return a Json here
// return {hello: 'Hello'};
}