JQuery does return data or status on non 200 HTTP Code

I’m working on an Ajax voting feature on my website. When a user clicks the like/dislike button, a request is sent to the server with request details. When a user isn’t logged in, the server returns a 401 Unauthorized:

The only issue with this is now I want users to be alerted that they have to sign in to vote. JQuery seems disregard data, status function on non 200 HTTP codes.

(none of the code under the POST request is run when a 401 is received.)

How can I fix this?

When I try, i don’t have any problem for the error message.

I’ve changed the code on the website since this post, i suppose the fix at the moment is to always return a 200 OK.

https://api.jquery.com/jquery.post/

the docs say that’s the ‘success’ callback. you should use the settings argument to provide an error callback for this case

https://api.jquery.com/jquery.ajax/#jQuery-ajax-settings

1 Like