I have a MVC4 application where I call a controller action from javascript using jQuery. When an exception occurs in the controller the returned response text is in HTML format. I want it to be in JSON format. How can this be achieved?
I thought some JSON formatter should do the magic on its own...
JavaScript
// Call server to load web service methods
$.get("/Pws/LoadService/", data, function (result) {
// Do stuff here
}, "json")
.error(function (error) { alert("error: " + JSON.stringify(error)) });
.Net Controller Action
[HttpGet]
public JsonResult LoadService(string serviceEndpoint)
{
// do stuff that throws exception
return Json(serviceModel, JsonRequestBehavior.AllowGet);
}