Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JsonResult produce 500 internal server error?

I am trying to retrieve a value from my Microsoft SQL Server database. It is a nullable "bit".

The code to retrieve

[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"); 
}

And the code to retrieve:

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

Why am i getting a 500 internal server error?

The debugger doesn't catch any exception.

like image 751
Kenci Avatar asked Jan 31 '12 14:01

Kenci


People also ask

Is 500 an internal server error?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

Why do internal server errors happen?

What causes a 500 Internal Server error. This error means there is a problem on the server side. A server error can be caused by any number of things from uploading the incorrect file to as bug in a piece of code. This error response is a generic "catch-all" response.


1 Answers

The problem is most likely because ASP.NET MVC does not allow JSON requests using GET by default. You can add JsonRequestBehavior.AllowGet as a second parameter to your Json call:

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

If not, can you provide a error message?

like image 87
alexn Avatar answered Sep 23 '22 05:09

alexn