Documente Academic
Documente Profesional
Documente Cultură
Asynchronous
JavaScript
and XML
Ajax nu este o tehnologie, este un
ansamblu de tehnologii:
fragment
JavaScript 1
reacioneaz la un eveniment eveniment
schimb un fragment din pagin
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// se trimite request la server
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
JavaScript 3
trimite
trimite request la server
// exemplu
xmlhttp.open("GET","demo_get2.asp?fname=Andrei&lname=
Frunza",true);
xmlhttp.send();
// alt exemplu
xmlhttp.open("POST","demo_post.asp",true);
xmlhttp.send();
JavaScript 3
trimite
trimite request la server
function loadText()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// se trimite request la server
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
4
ajax_info.txt server
AJAX va saluta!
AjAX
5
Server Response callback
processing
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 &&
xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML
=xmlhttp.responseText;
}
} // dac n request e fiierul textual
AjAX 6
Server Response (XML) DOM
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML=txt;
}
}
AjAX 4
Server Response (XML) server
<body>
<h2>My CD Collection:</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Get
my CD collection</button>
</body>
AjAX 6
Server Response DOM
Rezultat:
My CD Collection:
Bob Dylan
Bonnie Tyler
Dolly Parton
Gary Moore
Eros Ramazzotti
Bee Gees
...
AjAX 5
onreadystatechange callback
event
Proprietatea onreadystatechange specific
funcia care va fi apelat fiecare data cnd se
schimb proprietatea readyState
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 &&
xmlhttp.status==200)
{
...
}
}
AjAX
5
onreadystatechange callback
event
Proprietatea readyState:
<body>
</body> 1
</html> eveniment
Exemplu cu fisierul XML
<html>
<head>
<script type="text/javascript">
function loadXMLDoc(url)
{
var xmlhttp;
if (window.XMLHttpRequest) 2
{// code for IE7+, Firefox, Chrome, Opera, Safari object
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Exemplu cu fisier XML
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
5
document.getElementById('A1').innerHTML=xmlhttp.status;
callback
document.getElementById('A2').innerHTML=xmlhttp.statusText;
document.getElementById('A3').innerHTML=xmlhttp.responseText;
}
} 6
xmlhttp.open("GET",url,true); DOM
xmlhttp.send();
}
3
</script>
trimite
</head>
Exemplu cu fisier XML
<body>
</body>
</html>
1
eveniment
Exemplu cu fisier XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
4
server
Exemplu cu fisier XML
HTML fiier:
<body>
</body>
</html>
Exemplu cu un program PHP
<body>
</body>
</html>
Exemplu cu un program PHP
<html>
<head>
<script type="text/javascript">
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Exemplu cu un program PHP
<html>
<head>
<script type="text/javascript">
function showHint(str)
{
var xmlhttp; 2
if (str.length==0)
{ object
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Exemplu cu un program PHP
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
Exemplu cu un program PHP
xmlhttp.onreadystatechange=function() 5
callback
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
6
xmlhttp.open("GET","ajax_info.php?q="+str,true); DOM
xmlhttp.send();
}
</script> 3
</head> send
4
Exemplu cu un program PHP server
Google Maps
Gmail
Youtube
Facebook tabs
Mai multe exemple
http://test1.ru/ajax.html
http://test1.ru/ajax_3.html V
http://test1.ru/ajax_4.html
http://http://localhost/ajax_note_2015_MI141.html
http://http://localhost/ajax_jquery_note_IA142.html
http://http://test1.ru/ajax_rss.html
http://htmlweb.ru/ajax/example/
xmlhttp.onreadystatechange=function() Obinerea RSS
{ ajax_rss.html
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("rssOutput").innerHTML=xmlhttp.respo
nseText;
}
}
xmlhttp.open("GET","getrss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select onchange="showRSS(this.value)">
<option value="">Select an RSS-feed:</option>
<option value="Google">Google News</option>
<option value="MSNBC">MSNBC News</option>
</select>
</form>
<br />
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>
getrss.php
<?php
//get the q parameter from URL
$q=$_GET["q"];
$xml=("http://news.google.com/news?ned=us&topic
=h&output=rss");
}
elseif($q=="MSNBC")
{
$xml=("http://rss.msnbc.msn.com/id/3032091/device
/rss/rss.xml");
}