我有一个 MVC4 应用程序,我在其中使用 jQuery 从 javascript 调用控制器操作。当控制器中发生异常时,返回的响应文本为 HTML 格式。我希望它采用 JSON 格式。如何做到这一点?
我认为一些 JSON 格式化程序应该自己发挥作用......
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 控制器动作
[HttpGet]
public JsonResult LoadService(string serviceEndpoint)
{
// do stuff that throws exception
return Json(serviceModel, JsonRequestBehavior.AllowGet);
}