AndrewVA
AndrewVA

Reputation: 155

Handle error from MVC controller in jqGrid

How to return error as JSON from server in MVC and handle this error in jqGrid? In controller I use this

throw new Exception("message");

And in jqGrid I use

loadError: Error
////

And my function

function Error(xhr, st, err) {
            console.log(xhr.responseMessage);
        }

But there is a html code in xhr.responseMessage, and I need just my error message.

Upvotes: 0

Views: 1190

Answers (2)

dajo
dajo

Reputation: 1019

Just a small tweak to Wilts C's answer:

Server side

try {
    // some processing
}
catch (Exception e) {
    Response.StatusCode = (int)HttpStatusCode.BadRequest;
    return Json(new { Message = e.Message});
}

jqGrid

loadError: function(xhr, status, error) {
    alert(xhr.responseJSON.Message || "There was an unhandled problem!");
}

Upvotes: 1

Wilts C
Wilts C

Reputation: 1750

Server side

try {
    // some processing
}
catch (Exception e) {
    Response.StatusCode = (int)HttpStatusCode.BadRequest;
    return Json(new { Message = e.Message});
}

jqGrid

loadError: function(xhr, status, error) {
    alert(xhr.responseText);
}

Upvotes: 1

Related Questions