Anamnian Anamnian - 4 months ago 14
Ajax Question

Calling controller method via ajax reach "404 not found"

Got that error while try to call my controller's method (controller's name is

ProductsController
):

public ActionResult GetProducts()
{
return false;
}


Calling code like this:

$(document).ready(function () {
$.ajax({
type: 'POST',
url: '@Url.Action("GetProducts", "ProductsController")',
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf­8',
data: JSON.stringify(""),
})


Console in Chrome says:


jquery-1.10.2.js:8720 POST http
://localhost:56408/ProductsController/GetProducts 404 (Not Found)


Do you have any idea what is the problem?

Answer

Use the controller name prefix Products instead of ProductsController

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("GetProducts", "Products")',
        dataType: 'json',
        cache: false,
        contentType: 'application/json; charset=utf­8',
        data: JSON.stringify(""),
});

Asp.Net-MVC uses a naming convention for controllers.