Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To show JSON returned error message

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.

like image 707
VVR147493 Avatar asked Dec 16 '22 13:12

VVR147493


1 Answers

[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);

}
like image 184
Dave Alperovich Avatar answered Dec 24 '22 09:12

Dave Alperovich