在使用IIS 7.5网站的服务器中使用ajax调用控制器操作时,收到“ 500 Internal Server Error”消息。进程在localhost开发环境中工作正常。
该过程涉及一个Ajax调用,该调用将json对象发送到控制器action方法,该控制器然后再发送回json消息。我已经尝试在Routeconfig文件中创建自定义路由,以考虑iis站点名称。 IE,“ http://localhost:3000/home”与“ http:// {SiteName} / {defaultapplicationpage}”。
JS文件
$.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控制器
[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 }
);
}
}
预期结果是使该方法返回“ true”或“ false”语句。似乎没有处理ajax调用,并且收到500内部错误。
答案 0 :(得分:1)
尝试像这样在控制器中提供json并在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);
}
在您的视图脚本中,数据以这种方式使用:
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;
}
});