Trying to display/parse a jQuery $.get/$.ajax HTTP 400 Bad Request response

Viewed 771

I'm doing a simple jQuery $.get or $.ajax call to my API webserver (which has CORS enabled).

When I hit my endpoint /orders/search and fail to provide the correct/valid search filters, I return a nice 400 BAD REQUEST but I also return some json error message in the BODY of the response:

{
    "Errors": {
        "Query": "An order id is required. eg. ABCD-1234"
    }
}

I'm not sure how to retrieve this json payload when I use a jQuery ajax/get method to retrieve this information.

Here's the code I've got:

function doAjaxGet(endpoint, queryString, successFunction) {

    $.get(ApiWebsite + "/" + endpoint, queryString, function(data){
        if (successFunction != null) {
            successFunction(data);
        }
    })
    .fail(function(jqXHR, status, exception) {
        // This would be where I would actually parse the json payload 
        // for the failure reasons. This would usually be where the
        // validation failure messages would be displayed.
        $("#messageBox").text("error blah blah blah")
        .show()
        .delay(5000)
        .fadeOut();
    });
}

Edit

It was suggested that my question is a duplicate of this SO question. I checked that out and they suggest that to access the response payload I should do this:

$.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
    alert(jqXHR.responseText);
})

But the responseText property doesn't exist on my jqXHR instance.

Here's the picture of the jqXHR instance:

enter image description here

0 Answers
Related