What is `XMLHttpRequest` used for in AJAX?
A
To design XML documents
B
To send and receive HTTP requests/responses without reloading the page
C
To validate JavaScript code
D
To render CSS styles dynamically
Analysis & Theory
`XMLHttpRequest` is the core object used in AJAX to **send HTTP requests and receive responses** asynchronously.
Which method initializes a request in `XMLHttpRequest`?
Analysis & Theory
`open()` is used to **initialize a new request** and specify the method, URL, and async flag.
Which method actually sends the request to the server?
Analysis & Theory
`send()` is used to **send the request** after it has been opened and optionally configured.
What does `xhr.readyState === 4` mean?
B
The server has received the request
C
The operation is complete and response is ready
D
The response was invalid
Analysis & Theory
`readyState === 4` indicates the **request is complete** and the response is ready to be processed.
Which property of `XMLHttpRequest` holds the raw response as a string?
Analysis & Theory
`responseText` contains the **textual data** returned by the server.
Which HTTP status code indicates a successful request in `XMLHttpRequest`?
Analysis & Theory
`200 OK` means the request was successful and the server returned the expected response.
Which of the following events is commonly used to handle an AJAX response?
Analysis & Theory
`onload` is used to run JavaScript when the **response is successfully received**.
Which method sets a request header before sending a request?
Analysis & Theory
`setRequestHeader()` is used to **define custom HTTP headers** for the request before calling `send()`.
Which MIME type is used for sending XML data with `XMLHttpRequest`?
Analysis & Theory
`application/xml` is the appropriate **MIME type** for sending XML content to the server.
Which of the following is TRUE about `XMLHttpRequest`?
A
It can only be used with POST requests
B
It cannot handle JSON responses
C
It is used to send and receive data asynchronously from the server
D
It must always reload the page
Analysis & Theory
`XMLHttpRequest` supports **asynchronous communication**, improving performance and user experience.