Reputation: 61
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.
Upvotes: 0
Views: 1928
Reputation: 893
Try in your controller like this to serv json and used that in javascript
public async Task<JsonResult> DonutChartData()
{
int tagIn = (await db.Tag.Where(x => x.IsIn == true).ToListAsync()).Count;
int cardIn = (await db.Entry.Where(c => c.Type == Type.Card).Where(x => x.IsExit == false).ToListAsync()).Count;
int reservedIn = (await db.Cars.Where(c => c.Type == Type.Pin).Where(x => x.IsExit == false).ToListAsync()).Count;
DonutChart _chart = new DonutChart();
_chart.labels = new string[] { "x", "y", "x" };
_chart.datasets = new List<DonutChartDatasets>();
List<DonutChartDatasets> _dataSet = new List<DonutChartDatasets>();
_dataSet.Add(new DonutChartDatasets()
{
label = "Whois",
//TO-DO: Add Reserve to Report
data = new int[] { cardIn, tagIn, reservedIn },
backgroundColor = new string[] { "rgba(54, 162, 235,0.5)", "rgba(255, 205, 86,0.5)", "rgba(255,99,132,0.5)" },
borderColor = new string[] { "rgb(54, 162, 235)", "rgb(255, 205, 86)", "rgb(255,99,132)" },
borderWidth = "1"
});
_chart.datasets = _dataSet;
return Json(_chart, JsonRequestBehavior.AllowGet);
}
And in your view script used data in this way:
jQuery.extend({
getValues: function (url) {
var result = null;
$.ajax({
url: url,
type: 'get',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
result = data;
}
});
return result;
}
});
Upvotes: 1