Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 32113  / 12 Years ago, thu, february 21, 2013, 12:00:00

I needs to show the Json returned message.



In the controller, an exception is thrown and caught in a catch block. I am returning the fault error message.



In Ajax, the success part always executes. But if it is an error from my webservice, I don't want to execute the normal; instead I want to show an error message.



How I can achieve this?



My code below:



Controller



[HttpPost]
public JsonResult DeleteClientRecord()
{

bool result = true;
try
{
result = ClientCRUDCollection.DeleteClient(deleteClientId);

}
catch (Exception ex)
{

return Json(ex.Message, JsonRequestBehavior.AllowGet);
}

return Json(new { result }, JsonRequestBehavior.AllowGet);
}


AJAX Call



$(#YesDelete).click(function () {
$.ajax({
type: POST,
async: false,
url: /Client/DeleteClientRecord,
dataType: json,
error: function (request) {
alert(request.responseText);
event.preventDefault();
},
success: function (result) {
// if error from webservice I want to differentiate here somehow
$(#Update_ + id).parents(tr).remove();
$('#myClientDeleteContainer').dialog('close');
return false;
}
});

});


Please can anyone help me on this.


More From » jquery

 Answers
2
[HttpPost]
public JsonResult DeleteClientRecord()


{

bool result = true;
try
{
result = ClientCRUDCollection.DeleteClient(deleteClientId);
}
catch (Exception ex)
{
return Json(new { Success=False, responseText=ex.Message});
}

return Json(new { result }, JsonRequestBehavior.AllowGet);

}

[#80079] Wednesday, February 20, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalias

Total Points: 79
Total Questions: 116
Total Answers: 116

Location: Malaysia
Member since Wed, May 11, 2022
2 Years ago
;