I am trying to send a POST request with AJAX but getting status code 400 ("error"). This is my code.
namespace xx.WebApp.Pages.Dependency
{
public class CreateModel : PageModel
{
public IActionResult OnGet(string test) {
return Page();
}
[HttpPost]
public async Task<IActionResult> OnPostAsync([FromBody] CreateDependencyRequest createDependencyRequest) {
return new JsonResult("Test");
}
}
}
I troubleshoot it by sending a GET request and it works as expected. I test them by putting a breakpoint at the method.
This is how I send the GET request.
$.ajax({
url: `/Dependency/Create?test=1`,
type: 'GET',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(formData),
success: function (data) {
debugger
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
debugger
console.log(textStatus, errorThrown);
},
complete: function () {
createButton.prop('disabled', false);
}
});
This is how I send the POST request.
$.ajax({
url: `/Dependency/Create`,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(formData),
success: function (data) {
debugger
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
debugger
console.log(textStatus, errorThrown);
},
complete: function () {
createButton.prop('disabled', false);
}
});