In my ASP.NET Razor Pages project I want to do a POST request from JavaScript and use the response, without a page reload.
In my page model I have a handler similar to:
public async Task<IActionResult> OnPostAddThanks()
{
// actual work ommited
var thanksCount = 42; // dummy value
return new JsonResult(thanksCount);
}
I'm trying to call it from JavaScript with the fetch API doing:
async function sendThanks() {
const response = await fetch('?handler=AddThanks', {
method: 'POST'
})
return response.json()
}
This is pretty standard and straightforward, but it doesn't work, I get a status code 400, Bad Request, with no hint on what the problem is.
I spent several hours trying different things to get past this.