π§ͺ AJAX β Properties: readyState
, status
, responseText
, responseXML
Explained
π§² Introduction β Why Understand AJAX Response Properties?
When making asynchronous requests using AJAX, knowing how to handle the server’s response properly is critical. Properties like readyState
, status
, responseText
, and responseXML
help you track the state of the request, verify success, and read the returned data in various formats.
These properties give you full control over request lifecycle handling, error checking, and dynamic page updates.
π― In this guide, you’ll learn:
- What each of these core properties means
- How to use them correctly in real projects
- Code examples showing when and why to use them
- Common mistakes to avoid
π What Is readyState
in AJAX?
The readyState
property represents the current state of the AJAX request. Itβs used to monitor the progress of an asynchronous call.
π’ readyState Values:
Value | Description |
---|---|
0 | UNSENT β Request not initialized |
1 | OPENED β Server connection established |
2 | HEADERS_RECEIVED β Request received |
3 | LOADING β Processing request (partial data) |
4 | DONE β Request finished, response is ready β
|
β Example:
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log("Request complete.");
}
};
π What Is status
in AJAX?
The status
property gives the HTTP status code of the response. Itβs used to determine if the request was successful.
π’ Common HTTP Status Codes:
Code | Meaning |
---|---|
200 | OK β |
201 | Created |
204 | No Content |
400 | Bad Request |
401 | Unauthorized |
403 | Forbidden |
404 | Not Found β |
500 | Server Error β |
β Example:
if (xhr.readyState === 4 && xhr.status === 200) {
console.log("Success!");
}
π What Is responseText
in AJAX?
The responseText
property contains the raw response data from the server as a string. It’s typically used when the response is plain text, HTML, or JSON.
β Example β Reading Text Response:
xhr.onload = function () {
if (xhr.status === 200) {
console.log(xhr.responseText);
}
};
To parse JSON:
const data = JSON.parse(xhr.responseText);
console.log(data.username);
π What Is responseXML
in AJAX?
If the server returns XML and the response type is correctly set, responseXML
provides a parsed XML document you can query using DOM methods like getElementsByTagName
.
β Example:
xhr.onload = function () {
const xmlDoc = xhr.responseXML;
const name = xmlDoc.getElementsByTagName("name")[0].textContent;
console.log("Name:", name);
};
π Note: The server must return the correct content type (text/xml
or application/xml
) for this to work.
π§ͺ Full Example β Using All Four Properties
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.xml", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log("Text:", xhr.responseText); // raw text
console.log("XML:", xhr.responseXML); // parsed XML
}
};
xhr.send();
β This example checks request completion and logs both the raw response and XML content.
π¨ Common Mistakes to Avoid
β Mistake | β Fix |
---|---|
Checking status before readyState | Always check both readyState === 4 first |
Using responseXML on non-XML data | Only use when server returns XML |
Parsing responseText without checking status | Always verify status === 200 |
Using responseText for binary files | Use responseType = "blob" instead |
π Summary β Recap & Takeaways
The properties readyState
, status
, responseText
, and responseXML
are the core building blocks for handling AJAX responses effectively. They allow you to track request progress, verify success, and extract data from the response.
π Key Takeaways:
readyState
tracks request progress (0 to 4)status
helps confirm success (e.g., 200 OK)responseText
returns raw response as a stringresponseXML
provides parsed XML from the response
βοΈ Next Steps:
- Try building a small XML parser using
responseXML
- Use
responseText
withJSON.parse()
for JSON APIs - Learn how to use
responseType
for binary or blob responses
β FAQs β AJAX Properties
β What does readyState === 4
mean?
β
It means the request has completed, and the response is fully received and available for processing.
β Why is my responseXML
returning null?
β
This happens if the server doesn’t return Content-Type: text/xml
or if the response isn’t valid XML.
β Can I use responseText
with JSON?
β
Yes. You can parse it using JSON.parse(xhr.responseText)
.
β Is it necessary to check status
if readyState === 4
?
β
Yes. You must check both to confirm the request was successful:
if (xhr.readyState === 4 && xhr.status === 200)
β Whatβs the difference between responseText
and responseXML
?
β
responseText
gives the full raw response as a string; responseXML
parses XML into a DOM structure.
Share Now :