The error you are encountering is most likely due to an incorrect URL in your AJAX request. Let's take a look at the structure of your code and identify the issue.
public class ModelClass { public int Id { get; set; } public string Name { get; set; } }
This is the model class that represents the data you are sending in your AJAX request.
[Route("api/[controller]")] [ApiController] public class ManageEmployeeController : ControllerBase { [HttpPost] [Route("Insert_Something")] public async TaskInsert_Something(ModelClass searchModel) { return Ok(searchModel); } }
This is the API controller that handles the AJAX request. The [Route]
attribute specifies the URL that the controller responds to.
$(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) { } }); });
This is the AJAX request that is being sent to the API controller. The url
property specifies the URL that the request is being sent to.
Note:
window.location.origin +'/Api/ManageEmployee/Insert_Something'
is also a valid URL for the AJAX request.
Here is an example of the output that you should see in the console when the AJAX request is successful:
Note: If you have any specific requirements for custom route configuration, please refer to the official Microsoft documentation: Custom Route Configuration.