jmav
jmav

Reputation: 3139

HTTP headers for AJAX/ denied authentication?

I would like to know what is best practice with AJAX authentication process.

When authentication is valid I return HTTP header 200 with response "ok" What HTTP header do I need to send from server if authentication is NOT valid.

  1. Do I need to set HTTP header to code 500 (ERROR)
  2. or leave it on 200 and implement logic which checks response variable?

Upvotes: 2

Views: 242

Answers (2)

Jakub Konecki
Jakub Konecki

Reputation: 46008

How about returning HTTP401?

You can handle in in AJAX error handler and redirect the whole page to login screen, if it's your requirement.

$.ajax({
  statusCode: {
    401: function() {
      alert("User not logged in");
    }
  }
});

Upvotes: 1

Ben Davis
Ben Davis

Reputation: 13780

You don't want to send a 500 error, since that implies an unexpected server-side error that is not caused by the user.

You'll want to read up on the rfc spec for status codes:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

4XX status codes are for client errors, which is where you'll want to be looking. In your case, you could use 401 if authentication failed, and 403 if that user is not allowed to view the resource.

Upvotes: 2

Related Questions