javascriptc#ajaxmessagebad-request

How to get badrequest message in JS code


I can't find any solution, so I decided to ask this question. In my app when user tries to make more than 3 requests to rent a book I return in C# :

return BadRequest("Too many requests.");

In a browser concole I see that response was sent (status code 400) and a message is:

{message: "Too many requests."}

the thing is I can't figure out how to get this message in JS code in fail method.

.fail(function (respond) {
    alert(respond.message);
});

When I try: alert(respond) it shows: [object Object]

Do you have any ideas how to fix it ?


Solution

  • You can get your bad request message with:

    console.log(respond.responseJSON.Message);
    

    or

    var obj = JSON.parse(data.responseText);
    console.log(obj.Message);
    

    both are correct, but first example is better