Receiving a '500 Internal Server Error' message when using ajax to call controller action in server with IIS 7.5 web site. Process works fine in localhost development environment.
The process involves an Ajax call that sends a json object to the controller action method which then sends back a json message. I've already tried creating a custom route in the Routeconfig file to take into account the iis site name. IE, "http://localhost:3000/home" vs "http://{SiteName}/{defaultapplicationpage}.
JS File
$.ajax({
async: false,
type: "POST",
url: "/TimeEntryWeeklyReportsTest/Home/CheckIfRecordsExist",
//url: "/Home/CheckIfRecordsExist",
data: '{ data:' + jsondata + '}',
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function (response) {
console.log(response);
if (response === "true") {
var param = "&StartDate=" + data.StartDate + "&EndDate=" + data.EndDate;
param = Employeefilter !== undefined ? param + "&" + Employeefilter + "=" + data.EmployeeUserid : param + "&Employee=" + data.EmployeeUserid;
$('#successmsg').html("Successful");
window.location.href = url + param + "&rs:Format=" + documentType;
}
else {
$('#errmsg').html("No records found.");
throw 'records not found error';
}
}).fail(function (response) {
console.log('Error: ' + response);
});
CS Controller
[HttpPost]
[Route("TimeEntryWeeklyReportsTest/Home/CheckIfRecordsExist")]
public JsonResult CheckIfRecordsExist(FormData data)
{
string strID = GetIDFromUser((!String.IsNullOrEmpty(GetUser())) ? GetUser() : Environment.UserName);
var results = timeEntry.TimeEntryReport(data.EmployeeSupervisor == "Supervisor" ? null : data.EmployeeUserid, data.EmployeeSupervisor == "Employee" ? null : data.EmployeeUserid, Convert.ToDateTime(data.Startdate), Convert.ToDateTime(data.Enddate)).ToList<TimeEntryReport_Result>();
if (results.Count != 0)
{
return Json("true", JsonRequestBehavior.AllowGet);
}
else
{
return Json("false", JsonRequestBehavior.AllowGet);
}
}
RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CheckIfRecordsExist",
url: "TimeEntryWeeklyReportsTest/{controller}/{action}",
defaults: new { controller = "Home", action = "CheckIfRecordsExist" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The expected Result is to have the method return either a "true" or "false" statement. It seems that the ajax call is not handled and a 500 internal error is received.
async: false
. It's very bad practice and you don't need it as you've implemented promise callbacks anyway. – Rory McCrossan