AJAX - HTTP status codes
Categories of HTTP Status Codes
HTTP status codes are grouped by their first digit:
-
1xx (Informational) – Request received, still processing.
-
2xx (Success) – Request was successfully received and processed.
-
3xx (Redirection) – Client must take additional steps (like following a new URL).
-
4xx (Client Error) – Something is wrong with the client’s request.
-
5xx (Server Error) – The server failed to fulfill a valid request.
Common HTTP Status Codes
2xx – Success
-
200 OK – The request was successful, and the server returned the expected response.
-
Example: AJAX
GET /usersreturns JSON with user data.
-
-
201 Created – A resource was successfully created (common with
POST).-
Example: Submitting a form via AJAX to create a new account.
-
-
204 No Content – Request succeeded, but there’s no response body.
-
Example: Deleting a record with AJAX.
-
3xx – Redirection
-
301 Moved Permanently – Resource has a new permanent URL.
-
302 Found – Temporary redirection; resource is elsewhere for now.
-
304 Not Modified – Cached version is still valid (used in AJAX caching).
4xx – Client Errors
-
400 Bad Request – Server cannot process request due to invalid syntax.
-
Example: Sending malformed JSON in an AJAX request.
-
-
401 Unauthorized – Authentication required or failed.
-
Example: AJAX call without a valid token.
-
-
403 Forbidden – Client is authenticated but not allowed access.
-
Example: User tries to fetch admin-only data.
-
-
404 Not Found – The requested resource doesn’t exist.
-
Example: AJAX call to
/api/user/999when that user ID doesn’t exist.
-
-
408 Request Timeout – The request took too long.
5xx – Server Errors
-
500 Internal Server Error – Generic server-side failure.
-
Example: Database crash or unhandled exception in the backend.
-
-
502 Bad Gateway – Server acting as a gateway got an invalid response.
-
503 Service Unavailable – Server is overloaded or down for maintenance.
-
504 Gateway Timeout – A server taking too long to respond.
Why This Matters in AJAX
In JavaScript, you can detect these codes and respond accordingly:
fetch('/api/data')
.then(response => {
if (response.ok) { // true for 200–299
return response.json();
} else {
throw new Error(`Error ${response.status}: ${response.statusText}`);
}
})
.then(data => console.log(data))
.catch(error => console.error(error));