为什么JsonResult会产生500个内部服务器错误?

时间:2012-01-31 14:39:50

标签: asp.net-mvc asp.net-mvc-3

我正在尝试从Microsoft SQL Server数据库中检索值。它是一个可以为空的“位”。

要检索的代码

[HttpGet]
public JsonResult WishesVisit()
{
    int firmaid = SessionExtensions.GetFirmaId(Session);
    var firma = db.Firma.Where(x => x.firma_id == firmaid).FirstOrDefault();

    if (firma != null)
    {
        if (firma.oensker_besog != null)
        {
            if ((bool)firma.oensker_besog)
            {
                return Json("true");
            }
            else
            {
                return Json("false");
            }
        }
    }

    return Json("null"); 
}

要检索的代码:

$.getJSON('WishesVisit', function (data) {
    alert(data);
});

为什么我收到500内部服务器错误?

调试器没有捕获任何异常。

1 个答案:

答案 0 :(得分:17)

问题很可能是因为ASP.NET MVC默认情况下不允许使用GET的JSON请求。您可以将JsonRequestBehavior.AllowGet作为第二个参数添加到Json调用中:

return Json("true", JsonRequestBehavior.AllowGet);

如果没有,你能提供错误信息吗?