AJAX - Status Codes
Status Codes in AJAX represent the response status returned by the server after an AJAX request. They provide information about the outcome of the request, such as success, errors, redirects, and more. Understanding status codes is crucial for handling responses appropriately in AJAX applications.
HTTP Status Codes
Category | Status Code | Description |
---|---|---|
1xx - Informational | 100 | Continue: The server has received the request headers and the client should proceed to send the request body. |
101 | Switching Protocols: The server is switching protocols according to the Upgrade header field sent by the client. | |
102 | Processing: The server has received and is processing the request, but no response is available yet. | |
103 | Early Hints: Indicates to the client that the server is likely to send a final response with the header fields contained in the informational response. | |
104-199 | (Unused): Reserved for future use. | |
2xx - Success | 200 | OK: The request has succeeded. |
201 | Created: The request has been fulfilled and a new resource has been created. | |
204 | No Content: The server successfully processed the request, but there is no content to return. | |
205-299 | (Unused): Reserved for future use. | |
3xx - Redirection | 301 | Moved Permanently: The requested resource has been permanently moved to a new location. |
302 | Found: The requested resource has been temporarily moved to a different location. | |
304 | Not Modified: The resource has not been modified since the last request. | |
4xx - Client Error | 400 | Bad Request: The server cannot process the request due to malformed syntax. |
401 | Unauthorized: Authentication is required and has failed or has not yet been provided. | |
403 | Forbidden: The server understood the request but refuses to authorize it. | |
404 | Not Found: The requested resource could not be found on the server. | |
5xx - Server Error | 500 | Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request. |
502 | Bad Gateway: The server received an invalid response from an upstream server while acting as a gateway or proxy. | |
503 | Service Unavailable: The server is currently unable to handle the request due to temporary overloading or maintenance of the server. |
1. Handling Success (2xx) Status Codes
Success status codes (2xx) indicate that the request was successfully received, understood, and processed by the server.
Example:
// Handling success status code
$.ajax({
url: 'https://api.example.com/data',
method: 'GET',
success: function(response) {
console.log('Request successful:', response);
},
error: function(xhr, status, error) {
console.error('Error:', status, error);
}
});
In this example, the success callback function handles the response when the server returns a success status code (e.g., 200).
2. Handling Errors (4xx and 5xx) Status Codes
Error status codes (4xx and 5xx) indicate that there was an error processing the request on the server or the request cannot be fulfilled.
Example:
// Handling error status code
$.ajax({
url: 'https://api.example.com/invalidEndpoint',
method: 'GET',
success: function(response) {
console.log('Request successful:', response);
},
error: function(xhr, status, error) {
console.error('Error:', status, error);
}
});
In this example, the error callback function handles the response when the server returns an error status code (e.g., 404 or 500).
3. Redirects (3xx) Status Codes
Redirect status codes (3xx) indicate that further action is needed to complete the request, such as redirection to a different URL.
Example:
// Handling redirect status code
$.ajax({
url: 'https://api.example.com/redirect',
method: 'GET',
success: function(response) {
console.log('Request successful:', response);
},
error: function(xhr, status, error) {
console.error('Error:', status, error);
}
});
In this example, the error callback function handles the response when the server returns a redirect status code (e.g., 301 or 302).
4. Conclusion
Status Codes in AJAX provide valuable information about the outcome of requests, allowing developers to handle responses appropriately. By understanding and handling different status codes, developers can create robust and reliable AJAX applications.
Comments
Post a Comment