My ajax call:
$.ajax({
url: './Orders/SaveOrder',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
if (d.status == true) {
alert('Successfully saved.');
}
else {
alert('Failed' + d.ErrorMessage);
}
},
error: function (error) {
alert('Error. Please try again.');
}
And my SaveOrder controller action:
[HttpPost]
public JsonResult SaveOrder(OrderVM o)
{
try
{
if (ModelState.IsValid)
{
// do some database work
}
}
return new JsonResult { Data = new { status = status} };
}
catch (Exception ex)
{
// how to return jsonresult with error?
}
}
How do I get the ajax request to receive the details from the try-catch catch block? Specifically how does the 'error: function...' section get the code back?