Advertisement
piffy

AJAX 101 (parte JS - ajax.js)

Aug 13th, 2015
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function GetData() {
  2.     // 1. Istanziazione della richiesta (con versione IE 5-6)
  3.     var xhr;
  4.     if (window.XMLHttpRequest)
  5.         xhr = new XMLHttpRequest();
  6.     else if (window.ActiveXObject)
  7.         xhr = new ActiveXObject("Msxml2.XMLHTTP");
  8.     else
  9.         throw new Error("Ajax non รจ supportato dal browser");
  10.    // 2. Gestire la risposta del server
  11.     xhr.onreadystatechange = function () {
  12.         if (xhr.readyState < 4)
  13.             document.getElementById('div1').innerHTML = "Loading...";
  14.         else if (xhr.readyState === 4) {
  15.             if (xhr.status == 200 && xhr.status < 300)
  16.                 document.getElementById('div1').innerHTML = xhr.responseText;
  17.         }
  18.     }
  19.     // 3. Specificare metodo, URL e avviare le operaioni
  20.     xhr.open('GET', 'data.html');
  21.     xhr.send(null);
  22. }
  23. //alert("DEBUG: Script caricato correttamente");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement