Rohini 81451426554
Rohini 81451426554
Rohini 81451426554
The XMLHttpRequest object is used to exchange data with a server behind the scenes.
This means that it is possible to update parts of a web page, without reloading the whole page.
XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript, and
other web browser scripting languages to transfer and manipulate XML data to and from a
webserver using HTTP, establishing an independent connection channel between a webpage's
Client-Side and Server-Side. The data returned from XMLHttpRequest calls will often be
provided by back-end databases. Besides XML, XMLHttpRequest can be used to fetch data in
other formats, e.g. JSON or even plain text.
The XMLHttpRequest object calls the callback() function and processes the
result.
The HTML DOM is updated.
- JavaScript may now be used to modify the element's attributes; modify the
element's style properties; or add, remove, or modify the child elements.
<script type="text/javascript">
<!— functionsetMessageUsingDOM(message)
{ varuserMessageElement = document.getElementById("userIdMessage");
varmessageText;
if (message == "false")
{ userMessageElement.style.color = "red";
messageText = "Invalid User Id"; }
else {
userMessageElement.style.color = "green";
messageText = "Valid User Id"; }
varmessageBody = document.createTextNode(messageText);
if (userMessageElement.childNodes[0])
{ userMessageElement.replaceChild(messageBody, userMessageElement.childNodes[0]);
}
Else { userMessageElement.appendChild(messageBody); }}
</script> <body> <div id="userIdMessage"><div> </body>
XMLHttpRequest Methods
Method Description
abort() Cancels the current request.
open( method, URL ) Specifies the method, URL, and other optional
open( method, URL, async ) attributes of a request. The method parameter can
have a value of "GET", "POST", or "HEAD".
open( method, URL, async, userName ) Other HTTP methods, such as "PUT" and
open( method, URL, async, userName, "DELETE" may be possible. The "async"
password ) parameter specifies whether the request should be
handled asynchronously or not. "true" means that
the script processing carries on after the send()
method without waiting for a response, and
"false" means that the script waits for a response
before continuing script processing.
XMLHttpRequest Properties
onreadystatechange: An event handler for an event that fires at every state change.
readyState: The readyState property defines the current state of the XMLHttpRequest
object.
State Description
0 The request is not initialized.
1 The request has been set up.
2 The request has been sent.
3 The request is in process.
4 The request is completed.
readyState = 0 After the user have created the XMLHttpRequest object, but before
the call of the open() method.
readyState = 1 After the user have called the open() method, but before the call of
send().
readyState = 3 After the browser has established a communication with the server,
but before the server has completed the response.
readyState = 4 After the request has been completed, and the response data has
been completely received from the server.
Status:Returns the status as a number (e.g., 404 for "Not Found" and 200 for
"OK").
statusText:Returns the status as a string (e.g., "Not Found" or "OK").