filterContext.Result = new JsonResult() in above code(CustomJsonExceptionHandler) does not work properly when I access website from outside the server.
Please see below code, function is throwing an exception. Custom attribute is being used to return the error message to ajax function
controller function
[HttpPost]
[CustomJsonExceptionHandler]
public ActionResult GetList(int month, int year)
{
throw new ArgumentException("Exception");
List<Dictionary<string, object>> List = new List<Dictionary<string,object>>()
return Json(List, JsonRequestBehavior.AllowGet);
}
Custom error attribute
public class CustomJsonExceptionHandler : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.Exception != null)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.Result = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
errorMessage = filterContext.Exception.Message
}
};
}
}
}
Please see below code of jquery ajax function
$.fn.GetData = function (month, year) {
var data = null;
$.ajax({
url: '/HomeController/GetList',
data: { month: month, year: year },
type: 'POST',
async: false,
success: function (Listdata) {
data = Listdata;
},
error: function (xhr) {
var jsonerror = $.parseJSON(xhr.responseText)
alert(jsonerror.errorMessage)
}
});
return data;
}
Please Help !!
Server Error