Search code examples
javascriptjsonerror-handlingex

how to display exception response text


I have the following function, I am trying to display the response text that comes back from the ex.responseText. how ever every time I try it comes back as "undefined" but the text is actually there

onError: function (ex) {


            $('<div>' + ex._message + '</div>').dialog({
                modal: true,
                resizable: false,
                title: "Items",
                buttons: { "Okay": function () { $(this).dialog("close"); } }
            });
        }

so I tried the following

$('<div>' + ex.responseText + '</div>').dialog({
                    modal: true,
                    resizable: false,
                    title: "Items",
                    buttons: { "Okay": function () { $(this).dialog("close"); } }
                });

and it shows me the error like this {"message":"You have entered duplicate items. Please remove."}

I just want it to show the actual message You have entered duplicate items. Please remove. and not show the {} brackets and "message" text.

I also tried

 var message = JSON.parse(ex.responseText)._message;
 $('<div>' + message + '</div>').dialog({
            modal: true,
            resizable: false,
            title: "Items",
            buttons: { "Okay": function () { $(this).dialog("close"); } }
        });

and it still comes back as undefined.

what am I doing wrong?


Solution

  • Try ex.responseText.message, since the error message is an object you need to target the key.

    To put it more clear:

    ex.responseText = {
        "message": "You have entered duplicate items. Please remove."
    }
    
    ex.responseText.message = "You have entered duplicate items. Please remove.";