I work on asp.net razor , i get error when try make ajax request to access OnPostshiftDataUp
ERROR : Failed to load resource: the server responded with a status of 400 ()
console.log(formdata) // serialize data
serverip=172.16.224.999&printername=Zebra2&Location=SupervisorOffice&__RequestVerificationToken=CfDJ8DvYZnvxyLVKi-zvp3jzTImPtgQsnvTBz4ot8mbln2lEaqGcaT_gzZ-KNNfuEBoqHYSBIdcahXL3LLuum4nfTi2oZDQj-PfodyDAVpa45rYEIb7EcRvHrMn71AyB8PMC6DrqvT8q3cCEcSlt1sYQJ-M
but break point not catch this function meaning not detect there are breakpoint must catch it
the main purpose from ajax request is save data after update data on bootstrap model
what I try for my code
1- function with type post on page model
public ActionResult OnPostshiftDataUp()
{
return Content("msg");
}2- ajax request to access function or action OnPostshiftDataUp ajax request i do it
$('#save-btn').click(function () {
var formData = $('#edit-form').serialize();
console.log(formData);
$.ajax({
url: '?handler=shiftDataUp',
type: "POST",
data: JSON.stringify(formData),
success: function (result) {
$('#edit-modal').modal('hide');
location.reload();
},
error: function (xhr, status, error) {
console.log(error);
}
});
});bootstrap model then send ajax request when click button save
@**@
Hi,@TempData["UserID"]
×
@* *@
Server IP
Printer Name
Location Name
@*
Server IP
Close
Save changes
Amit MohantyRank #112 · 20K reputationPosted Jun 22, 2023, 7:36 AM EDT
You have set the url parameter to '?handler=shiftDataUp'. However, the correct URL should be '/?handler=shiftDataUp'.
You are serializing the form data using $('#edit-form').serialize(). There is no need to convert it to JSON using JSON.stringify. So you can remove JSON.stringify from your Ajax request.
$('#save-btn').click(function () { var formData = $('#edit-form').serialize(); $.ajax({ url: '/?handler=shiftDataUp', type: "POST", data: formData, success: function (result) { $('#edit-modal').modal('hide'); location.reload(); }, error: function (xhr, status, error) { console.log(error); } }); });Deepak RawatRank #646 · 3.7K reputationPosted Jun 22, 2023, 7:46 AM EDT
Check the server-side code:
OnPostshiftDataUpmethod in your ASP.NET Razor page is properly defined and decorated with the[HttpPost]attribute.Verify the AJAX request:
OnPostshiftDataUpmethod.formdatavariable contains the expected serialized data by logging it to the console.Include the necessary request headers:
__RequestVerificationToken), make sure it is included in the AJAX request headers.Content-TypeandX-Requested-With, as required by your server-side code.ahmed salahRank #2,240 · 628 reputationPosted Jun 22, 2023, 11:47 AM EDT
until now not hit or catch function on run time
i do as you told me above but no error and no catch for method
public ActionResult OnPostshiftDataUp() { return Content("msg"); }
so what i do to solve issue
ahmed salahRank #2,240 · 628 reputationPosted Jun 23, 2023, 11:24 AM EDT
after apply the answe rabove issue not solved but i try with another way and it not catched function shiftUpData
I put break point on public IActionResult OnPostshiftDataUp
but breakpoint not catched or hit so what I do to solve issue
program.cs builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN"); public class ShelfLabelPrinterSetUpModel : PageModel { [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"); } } Hi,@TempData["UserID"] × Server IP Printer Name Location Name Save changes Close ID BranchType UserPC PrinterName ArabicConfig Status Location Name >Action @foreach (DataRow row in table.Rows) { @row["AutoID"] @row["BranchType"] @row["UserPC"] @row["PrinterName"] @row["ArabicConfig"] @row["Status"] @row["DisplayName"] Edit } @section scripts { $(document).ready(function(){ $('#editbootstarp tbody').on('click', 'tr', function () { // Get the data from the clicked row var rowData = $(this).children('td').map(function () { return $(this).text(); }).get(); $('#editshelflabelModal #edit-ip').val(rowData[2]); $('#editshelflabelModal #edit-printername').val(rowData[3]); $('#editshelflabelModal #edit-Location').val(rowData[6]); $('#editshelflabelModal').modal('show'); }); $('#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); } }); }); }); });Result of apply all above
breakpoint not hit or catch function on page model public IActionResult OnPostshiftDataUp and this is main issue
console.log(formdata)
serverip=172.16.224.998&printername=Zebra322&Location=DataEntry&__RequestVerificationToken=CfDJ8DvYZnvxyLVKi-zvp3jzTIkAYo94jN3ZNgSd1WmQBm2pv4zlgwNBawaJhzG4sROj2NQ7mDgAU4i-p_1BHUntZg9wNzm8povXvcBehss3xnOJ_yMmRMDuSweMRPCVVXS3L3Q8owl8ZURB3T_R2Zs0aTo
also success request
but not catch post method shiftDataUp
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 23, 2023, 1:33 PM EDT
Check that the routing configuration in your ASP.NET application is properly set up to handle the `OnPostshiftDataUp` method. Check if the route is correctly defined and that it matches the URL you are using in the AJAX request (`/?handler=shiftDataUp`). Check that the handler name in the AJAX request (`shiftDataUp`) matches the handler name in the page model (`OnPostshiftDataUp`). They should have the same name. Ensure that the Antiforgery token is correctly included in the AJAX request. In your JavaScript code, you are setting the XSRF-TOKEN header, which is good. However, make sure that the hidden input field for the __RequestVerificationToken is present in the HTML form. You can add it by including the @Html.AntiForgeryToken() helper method inside your