ERROR : Failed to load resource: the server responded with a status of 400 ()
For the 400 error, it might relate the Antiforgery. The Razor Pages is automatically protected from XSRF/CSRF. You should set the XSRF-TOKEN when you want to use the ajax to send request in razor page.
Check the Program.cs file (asp.net 6+ application), use the following code to register the Antiforgery and set the XSRF-TOKEN header.
builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
Then, in the razor page, before send the request, add the XSRF-TOKEN header. Check the following code:
Note: I changed the handler's name to "ShiftDataUp", and when send the data, without using the JSON.stringify
method to convert to data to json.
$('#save-btn').click(function () {
var formData = $('#edit-form').serialize();
console.log(formData);
$.ajax({
url: '?handler=ShiftDataUp',
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: formData,
success: function (result) {
alert(result);
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
You can check the test page source as below:
@page "/index1"
@model RazorWebApp.Pages.Index1Model
<form id="edit-form" method="post" >
<div class="modal-body">
@* <input type="hidden" id="edit-id" name="ShiftId">*@
<div class="form-group">
<label for="edit-server">Server IP</label>
<input type="text" class="form-control" id="edit-ip" name="serverip" value="123.456.789.123">
</div>
<div class="form-group">
<label for="edit-printer">Printer Name</label>
<input type="text" class="form-control" id="edit-printername" name="printername" value="Hello">
</div>
<div class="form-group">
<label for="edit-locationsdata">Location Name</label>
<input type="text" class="form-control" id="edit-Location" value="save" name="Location">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="save-btn">Save changes</button>
</div>
</form>
@section Scripts{
<script>
$(function(){
$('#save-btn').click(function () {
var formData = $('#edit-form').serialize();
console.log(formData);
$.ajax({
url: '?handler=ShiftDataUp',
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: formData,
success: function (result) {
alert(result);
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
});
</script>
}
Index1.cshtml.cs file:
public class Index1Model : PageModel
{
public void OnGet()
{
}
[BindProperty]
public string serverip { get; set; }
[BindProperty]
public string printername { get; set; }
[BindProperty]
public string Location { get; set; }
public IActionResult OnPostShiftDataUp(string serverip, string printername, string location)
{
return new JsonResult("Success");
}
}
The result as below:
If you are meeting the 404 error, the error relates the request url, check it and share with us.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion