Carlos Carlos - 5 months ago 24
jQuery Question

MVC AJAX QUERY Callback not working

Im trying to make a simple Jquery Ajax callback:

This is my Jquery code:

$(document).ready(function () {
$('#btnClient').click(function (e) {
e.preventDefault();
var txtClient1 = $('#txtClient').val();

$.ajax({
type: "POST",
url: '/VistasController/AROrders',
contentType: "application/json; charset=utf-8",
data: { txtClient: txtClient1},
dataType: "json",
success: successFunc,
error: errorFunc
});

function successFunc(data) {
alert("Sucess");
}

function errorFunc(result) {
alert("Error" + result.responseText);
}
});
});


This is my ASP.net c# code behind:

[HttpPost]
public ActionResult AROrders(string txtClient)
{
DataTable dtClients = clientData(txtClient);

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dtClientes.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dtClients.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
string ResultadoJSON = serializer.Serialize(rows);

return this.Content(ResultadoJSON, "application/json");

}


When I do a normal postback it works but with Jquery I get the error "Resource not found"

I have tried configuring the route in global.asax

routes.MapRoute(
"",
"{controller}/{action}/{id}",
new { controller = "Vistas", action = "AROrders", txtClient = UrlParameter.Optional }
);


I have also tried changing the URL without success.

Is there anything wrong with the Jquery?

Answer

Dont specify the postfix Controller in your URL. So instead of :

url: '/VistasController/AROrders',

chnage it to

url: '/Vistas/AROrders',