Getting responseText error values out of JQuery error?

500 views Asked by At

In my ajax .error function I'm trying to get out the values from the responseText being sent back from the API I'm getting for example

{"error":"Wrong Crendtials","error_description":"Provided username and password is incorrect"}

In my JavaScript its recognising there's errors, but I can't get it to get into the individual items which I want to get to show messages based on the error response?

).error(function (xhr, status, err) {
        var errorData = xhr.responseText;
        if (errorData != null) {
            alert("has errors");
           alert("error - " + errorData.error + " - desc " + errorData.error_description);
        }
    });

I'm sure this is something really basic, but I'm struggling to find an answer that breaks the values down for me?

Thanks

1

There are 1 answers

0
slowlygettingthere On

With many thanks to Swati. This was resolved with:

var jsonResponse = jQuery.parseJSON(xhr.responseText);
alert("Error: " + jsonResponse.error + " Description: " + jsonResponse.error_description);