Steps of AJAX Operation
Steps of AJAX Operation
Ajax Flow
function initRequest() {
if (window.XMLHttpRequest) {
} else if (window.ActiveXObject) {
isIE = true;
}}
function validateUserId() {
initRequest();
req.onreadystatechange = processRequest;
req.send(null);
Prepared by Krishna Srikanth M
}
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid>true</valid>");
} else {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid>false</valid>");
function and processes the result The XMLHttpRequest object was configured to call
the processRequest() function when there is a state change to the readyState of the
XMLHttpRequest object
function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
JavaScript technology gets a reference to any element in a page using DOM API
JavaScript technology may now be used to modify the element's attributes; modify
the element's style properties; or add, remove, or modify child elements
<script type="text/javascript">
function setMessageUsingDOM(message) {
var messageText;
if (message == "false") {
userMessageElement.style.color = "red";
Prepared by Krishna Srikanth M
messageText = "Invalid User Id";
} else {
userMessageElement.style.color = "green";
if (userMessageElement.childNodes[0]) {
userMessageElement.replaceChild(messageBody,
userMessageElement.childNodes[0]);
} else {
userMessageElement.appendChild(messageBody);
</script>
<body>
<div id="userIdMessage"></div>
</body>
<script type="text/javascript">
function setMessageUsingDOM(message) {
var messageText;
if (message == "false") {
userMessageElement.style.color = "red";
} else {
userMessageElement.style.color = "green";
if (userMessageElement.childNodes[0]) {
userMessageElement.replaceChild(messageBody,
userMessageElement.childNodes[0]);
} else {
userMessageElement.appendChild(messageBody);
</script>
<body>
<div id="userIdMessage"></div>
</body>
XMLHttpRequest Methods
send(content)
abort()
getAllResponseHeaders()
getResponseHeader(“header”)
setRequestHeader(“label”,”value”)
XMLHttpRequest Properties
onreadystatechange
change
0 = uninitialized
1 = loading
Prepared by Krishna Srikanth M
2 = loaded
4 = complete
status
responseText
responseXML
statusText
Mozilla FireBug debugger (add-on) : This is the most comprehensive and most
useful JavaScript
debugger