i’ve been struggling with this problem for quite some time. I’ve followed all suggestions provided in similar problems but without success.
As the title says, data passed by my $.ajax is not received by my controller.
jQuery:
var data = {
id: id,
app: app
};
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog",
data: JSON.stringify(data),
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
Controller:
//id and app receives null values
public ActionResult FetchTransactionLog(string id,string app) {
UtilityModels util = new UtilityModels(app);
List<ResultModel> result = util.FetchTransactionLog(id);
return View("LogResult", result);
}
Route in Global.asax:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Utility", action = "Index", id=UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"FetchLog",
"Utility/FetchTransactionLog/{id}/{app}",
new { controller = "Utility", action = "FetchTransactionLog", id = "", app = "" } // Parameter defaults
);
What am i doing wrong?
Update
The following works as suggested by Samich:
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog/" + id + "/" + app,
data: "{}",
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
try just to append your id and app to your url as you specified in the routes
/Utility/FetchTransactionLog/{id}/{app}Probably because your routing expects values in query string and doesn’t find it there, so the default values provided from the routing registration