Complete code for making an AJAX request and utilizing it .

function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}

  var http3 = getXMLHTTPRequest();
  
  
function startFinally() {
var myurl = "demo.php";
 var myurl1 = myurl;
  myRand = parseInt(Math.random()*999999999999999);
  var modurl = myurl1+"?rand="+myRand ;

  http3.open("GET", modurl, true);
  http3.onreadystatechange = useHttpResponse;
  http3.send(null);
}


function useHttpResponse() {
   if (http3.readyState == 4) {
    if(http3.status == 200) {
      var mytext = http3.responseText;
	 
		document.getElementById('myDivName').innerHTML = mytext;

		}
	 }
	 else {
         document. getElementById('myDivName').innerHTML = "Loading.....";
        }
    } 

  
	

In case if you are using more than one AJAX request on a page, you are advised to make a new XMLHttpRequest object for each of the functions using Ajax so that in case if the user clicks two of the links both respond irrespective of each other.

Once we are done with the basics of AJAX, lets see how can we use it to make a simple chat engine.

Member Login

Member Login




Not a Member? Sign Up!




Login to comment

Be the first to comment on this topic







  


<<< Wanna review

Continue >>>