Notification texts go here Contact Us Buy Now!

ajax requesting POST error status code 500

Certainly, below is a comprehensive response to guide you through the provided code snippet and resolve the 500 Internal Server Error you're encountering.

As per your code structure, the primary issue seems to lie within the incorrect AJAX URL. The route definition generates an invalid URL for reaching the API controller endpoint, resulting in a 500 status code.

To address this, let's consider the following:

Request Model:

        
public class ModelClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}
        
      

Controller:

Assuming you have the following API controller:

        
[Route("api/[controller]")]
[ApiController]
public class ManageEmployeeController : ControllerBase
{

    [HttpPost]
    [Route("Insert_Something")]
    public async TaskIActionResult Insert_Something(ModelClass searchModel)
    {

        return Ok(searchModel);
    }
}
        
      

Ajax Request Format:

To submit a request to the controller, decorate your AJAX request as follows:

        
$(document).on('click', '#btnSave', function (e) {

    const array = { Id: 1, Name: "Test Name" };
    var data = JSON.stringify(array);
    $.ajax({
        url: '/Api/ManageEmployee/Insert_Something',
        type: 'POST',
        data: data,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            console.log(result);
        },
        error: function (e) {
        }
    });
});
        
      

Note: You can also use window.location.origin +'/Api/ManageEmployee/Insert_Something' for the URL.

Output:

Upon successful submission, you should see the following output in the console:

Note: If you have specific requirements for custom route configuration, refer to the official documentation: Problems with Route Value Invalidation .

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.