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
Tuhin PaulPosted Jun 23, 2023, 5:33 PM
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
ahmed salahPosted Jun 23, 2023, 3:24 PM
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
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
ahmed salahPosted Jun 22, 2023, 3:47 PM
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
Deepak RawatPosted Jun 22, 2023, 11:46 AM
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.Amit MohantyPosted Jun 22, 2023, 11:36 AM
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.