XmlHttpRequest - Come fare la chiamata
Ad oggi l'oggetto XmlHttpRequest è supportato inInternet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, eNetscape 7. Questo oggetto non è definitocome standard dal W3C, il quale definisce alcunefunzionalità simili ma non ancora implementate inalcun browser.
Per questo motivo il W3C stesso è costretto asuggerire a chiunque abbia la necessità di effettuareuna HTTP request lato browser, di utilizzarel'oggetto XmlHttpRequest.
Cito testualmente dal w3c:
"The W3C DOM Level 3 "Load and Save"specification contains some similar functionality, but theseare not implemented in any browsers yet. So, at the moment,if you need to send an HTTP request from a browser, you willhave to use the XMLHttpRequest object."
Partiamo dunque dalla creazione dell'oggetto che varia aseconda del browser:
Per Mozilla, Firefox, Safari, and Netscape:
var xmlhttp=new XMLHttpRequest()
Per Internet Explorer (Solo fino alla versione 6 inclusa. Laversione 7 supporta anch'essa la classe XMLHttpRequest):
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
si rende quindi necessario scrivere il nostro codice di modoche quaqlsiasi browser possa creare e gestirecorrettamentel'oggetto:
<script type="text/javascript">var xmlhttpfunction loadXMLDoc(url){// codice per Mozilla, etc.if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest() xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("GET" ,url,true) xmlhttp.send(null) }// codice per IExploreelse if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") if (xmlhttp) { xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("GET" ,url,true) xmlhttp.send() } }}function xmlhttpChange(){// if xmlhttp shows "loaded"if (xmlhttp.readyState==4) { // if "OK" if (xmlhttp.status==200) { // ...il vostro codice... } else { alert("Impossibile ricevere i dati") } }}</script>
La stessa cosa diventa lievemente diversa con vbscript(esempio preso dahttp://www.w3schools.com/xml/tryit.asp?filename=try_xmlhttprequest_vb):
<script type="text/vbscript">dim xmlhttpfunction loadXMLDoc(url)set xmlhttp=createObject("Microsoft.XMLHTTP")xmlhttp.onreadystatechange=getRef("state_Change")call xmlhttp.open("GET" ,url,true)call xmlhttp.send()end functionfunction state_Change()if xmlhttp.readyState=4 then if xmlhttp.status=200 then alert("XML data OK") document.getElementById("A1").innerText=xmlhttp.status document.getElementById("A2").innerText=xmlhttp.statusText document.getElementById("A3").innerText=xmlhttp.responseText else alert("Problem retrieving XML data:" & xmlhttp.statusText) end ifend ifend function</script></head><body onload="loadXMLDoc('note.xml')"><h2>Using the HttpRequest Object</h2><p><b>status:</b><span id="A1"></span></p><p><b>status text:</b><span id="A2"></span></p><p><b>response:</b><br><span id="A3"></span></p></body></html>
Ovviamente sarebbe preferibile perseguire la prima stradavia javascript, quest'ultimo esempio è statoinserito solo a titolo informativo.
- Articolo precedente Scopriamo il linguaggio di programmazione Ajax e i suoi vantaggi
- Articolo successivo Creare un form
