Hello everyone I have faced an issue I have call a ajax with razor .net core.
public void OnGet() This is my GetFunction
{
}
I have called this function by ajax It's called successfully. But I have called a Public void OnPost() not called this by ajax.
$.ajax({
type: "POST", -- Not Called
url: "/Shared/_Partial?handler=InsertEmailBlastDetails",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: record,
success: function (data) {
}
$.ajax({
type: "GET", -- Called
url: "/Shared/_Partial?handler=InsertEmailBlastDetails",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: record,
success: function (data) {
}

Rajkiran SwainRank #165 · 14.4K reputationPosted May 16, 2023, 6:56 AM EDT
Try changing the URL in your AJAX call to include the handler name for the OnPost method:
$.ajax({ type: "POST", url: "/Shared/_Partial?handler=InsertEmailBlastDetailsOnPost", contentType: "application/json; charset=utf-8", dataType: "json", data: record, success: function (data) { // handle success response }, error: function (jqXHR, textStatus, errorThrown) { // handle error response } });Also make sure that you have a method named "InsertEmailBlastDetailsOnPost" with the [HttpPost] attribute in your Razor page.
Mudzakkir TohaRank #3,303 · 435 reputationPosted May 21, 2023, 9:35 AM EDT
based on this post, you should rename OnPost method into
OnPostInsertEmailBlastDetailsAsynchttps://www.learnrazorpages.com/razor-pages/handler-methods
public async Task OnPostRegisterAsync() { //…your core maybe return json }Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:07 AM EDT
The reason why your OnPost() method is not being called by Ajax is because you are using the GET method instead of the POST method. The GET method is used to retrieve data from a server, while the POST method is used to send data to a server. In your case, you are trying to send data to the server, so you need to use the POST method.
To fix this, change the type of your Ajax request to POST:
$.ajax({ type: "POST", url: "/Shared/_Partial?handler=InsertEmailBlastDetails", contentType: "application/json; charset=utf-8", dataType: "json", data: record, success: function (data) { } });Once you have made this change, your OnPost() method should be called when you submit the Ajax request.
Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:10 AM EDT
Based on the given code snippet, please check my comments:
Part 1
1. Use the `@Url.Action` helper method to generate the URL for the Ajax request. This way, you can avoid hard-coding the URL and ensure that the correct URL is used even if the application's routing configuration changes.
$.ajax({ type: "POST", url: '@Url.Action("_Partial", "Shared", new { handler = "InsertEmailBlastDetails" })', contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(record), success: function (data) { // Handle the success response }, error: function (xhr, textStatus, errorThrown) { // Handle the error response } });Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:11 AM EDT
Part 2
2. If you are using ASP.NET Core, consider using the `ValidateAntiForgeryToken` attribute on your `OnPost` method to protect against CSRF attacks. You will need to generate and include an anti-forgery token in your Ajax request. Here's an example of how to do it:
// Add the anti-forgery token to the Ajax request var token = $('input[name="__RequestVerificationToken"]').val(); $.ajax({ type: "POST", url: '@Url.Action("_Partial", "Shared", new { handler = "InsertEmailBlastDetails" })', contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(record), headers: { "__RequestVerificationToken": token }, success: function (data) { // Handle the success response }, error: function (xhr, textStatus, errorThrown) { // Handle the error response } });Tuhin PaulRank #7 · 71.9K reputationPosted May 21, 2023, 10:12 AM EDT
Part 3
3. Consider using the `done()` and `fail()` methods instead of the `success` and `error` callbacks for the Ajax request. This allows for cleaner code and better error handling.
$.ajax({ type: "POST", url: '@Url.Action("_Partial", "Shared", new { handler = "InsertEmailBlastDetails" })', contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(record) }).done(function (data) { // Handle the success response }).fail(function (xhr, textStatus, errorThrown) { // Handle the error response });These optimizations can help improve the readability, maintainability, and security of your code.