简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM