<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
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");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
Metoda open()
- pregateste obiectul XMLHTTPRequest pentru a comunica cu serverul si are cel putin 2 argumente obligatorii:
Dupa ce ati pregatit obiectul cu metoda open(), puteti trimite cererea cu metoda send().
<html>
<head>
<script>
function loadXMLDoc()
{
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");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
Let AJAX change this text
- Obiectul XMLHTTPRequest
- este acceptat de aproape toate browserele moderne, si este suportat de o multitudine de platforme.
Scopul obiectului XMLHTTPRequest este de a permite ca JavaScript sa formuleze cereri HHTTP si sa le trimita catre server. Aplicatiile web programate in mod traditional relizeaza de obicei astfel de cereri in mod sincron, ceea ce are ca rezultat transmiterea catre browser a unei pagini noi sau actualizate.
Scopul obiectului XMLHTTPRequest este de a permite ca JavaScript sa formuleze cereri HHTTP si sa le trimita catre server. Aplicatiile web programate in mod traditional relizeaza de obicei astfel de cereri in mod sincron, ceea ce are ca rezultat transmiterea catre browser a unei pagini noi sau actualizate.
Utilizand insa obiectul XMLHTTPRequest puteti pune pagina sa efectueze astfel de apelari in mod asincron, in fundal, ceea ce va permite utilizarea paginii fara intreruperi cauzate de reimprospatarea browserului.
Pentru majoritatea browserelor care accepta XMLHTTPRequest ca un obiect nativ(Mozilla, Opera si restul), crearea unei instante a aecestui obiect este simpla si directa:
Pentru majoritatea browserelor care accepta XMLHTTPRequest ca un obiect nativ(Mozilla, Opera si restul), crearea unei instante a aecestui obiect este simpla si directa:
var request = new XMLHTTPRequest();
Pentru Microsoft Internet Explorer trebuie sa creati un obiect ActiveX:
var request = new ActiveXObject("Microsoft.XMLHTTP")
| Method | Description |
|---|---|
| open(method,url,async) | Specifies the type of request, the URL, and if the request should be handled asynchronously or not. method: the type of request: GET or POST url: the location of the file on the server async: true (asynchronous) or false (synchronous) |
| send(string) | Sends the request off to the server. string: Only used for POST requests |
Metoda open()
- pregateste obiectul XMLHTTPRequest pentru a comunica cu serverul si are cel putin 2 argumente obligatorii:
- precizarea metodei HTTP pe care intentionati sa o utilizati GET sau POST,
- adresa URL de destinatie a cererii, pentru GET aceasta adresa trebuie sa fie codificata corespunzator.
Din motive de securitate, obiectului XMLHTTPRequest i se permite sa comunice doar cu adrese URL din interiorul propriului domeniu.
The url parameter of the open() method, is an address to a file on a server:
xmlhttp.open("GET","ajax_test.asp",true);
The file can be any kind of file, like .txt and .xml, or server scripting files like .asp and .php (which can perform actions on the server before sending the response back).
Metoda send()
Dupa ce ati pregatit obiectul cu metoda open(), puteti trimite cererea cu metoda send().

Niciun comentariu:
Trimiteți un comentariu