Ajax
Ajax
Ajax
What is AJAX?
AJAX = Asynchronous JavaScript And XML.
Property Description
Example:
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
xhttp.open("GET", "demo_get2.asp?
fname=Henry&lname=Ford", true);
xhttp.send();
To add an HTTP header with setRequestHeader(). Specify
the data you want to send in the send() method:
Asynchronous:
xhttp.open("GET", "ajax_test.asp", true);
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" >Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRexquest();
http. {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
ajax_info.txt
<h1>AJAX</h1>
<p>AJAX is not a programming language.</p>
<p>AJAX is a technique for accessing web servers from a
web page.</p>
<p>AJAX stands for Asynchronous JavaScript And
XML.</p>
cd_catalog.xml
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
</CATALOG>
ajaxdemo.html
<html>
<style>
table,th,td {
border : 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 5px;
}
</style>
<body>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "cd_catalog.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Artist</th><th>Title</th></tr>";
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("ARTIST")
[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("TITLE")
[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>
<script>
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x;
var txt = "";
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt += x.nodeValue + "<br>";
x.nodeValue = "Easy Cooking";
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt += x.nodeValue + "<br>";
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("book")[0]
var y = x.getAttributeNode("category");
var txt = y.nodeValue + "<br>";
y.nodeValue ="food";
txt += y.nodeValue;
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
XML DOM Removing Nodes
The removeChild() method removes a specified node.
When a node is removed, all its child nodes are also removed.
The below code will remove the first <book> element from the loaded
books.xml:
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var root = xmlDoc.documentElement;
var currNode = root.childNodes[1];
removedNode =
currNode.removeChild(currNode.childNodes[1]);
document.getElementById("demo").innerHTML =
"Removed node: " + removedNode.nodeName;
}
</script>
</body>
</html>
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("book");
document.getElementById("demo").innerHTML =
x[0].getAttribute('category') + "<br>";
x[0].removeAttribute('category');
document.getElementById("demo").innerHTML +=
x[0].getAttribute('category');
}
</script>
</body>
</html>
XML DOM Creating Nodes
The createElement() method creates a new element
node.
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp. {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, y, i, newElement, txt, xmlDoc;
xmlDoc = xml.responseXML;
newElement = xmlDoc.createElement("edition");
x = xmlDoc.getElementsByTagName("book")[0]
x.appendChild(newElement);