I'm studying javascript methods but I have difficult to make POST method works. Below you can see the code that I use to test:
<form id="form_test">
<h5>POST Method</h5>
<input type="text" name="text" id="text_input">
<button type="button" onclick="Save()">Send</button>
</form>
function Save() {
var xhr = new XMLHttpRequest();
var url = "/Page1?handler=Save";
xhr.open("POST", url, true);
let data = `{
"Id": 78912,
"Customer": "Jason Sweet",
"Quantity": 1,
"Price": 18.00
}`;
xhr.send(data);
}
public IActionResult OnPostSave()
{
return new JsonResult("my result");
}
Any idea what can do wrong?
You need to fix the URL where you want to send the post request. Also need to use [HttpPost] attribute for the action method.
Below is link to an example which uses XMLHttpRequest to POST data to method