Ajax Programming


var http_request = false;
  var ser = Math.round(Math.random()*1000000); // Anti-caching serial number
  var debug = false; // Set to true to show the full server response
  
  function ajax(httpRequestMethod, url, parameters, target)
  {
    http_request = false;
    document.getElementById(target).innerHTML = 'Wait...'
    if (window.XMLHttpRequest)
    { // For Mozilla, Safari, Opera, IE7
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
        http_request.overrideMimeType('text/plain');
        //Change MimeType to match the data type of the server response.
        //Examples: "text/xml", "text/html", "text/plain"
      }
    }
    else if (window.ActiveXObject)
    { // For IE6
      try
      {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {}
      }
    }
    if (!http_request)
    {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = function() {updateElement(target);};
    if (httpRequestMethod == 'GET')
    {
      http_request.open('GET', url + '?' + parameters, true);
      http_request.send(null);
      ser = ser + 1;
    }
    else if (httpRequestMethod == 'POST')
    {
      http_request.open('POST', url, true);
      http_request.setRequestHeader('Content-Type',
        'application/x-www-form-urlencoded');
      http_request.send(parameters);
    }
    else
    {
      alert('Sorry, unsupported HTTP method');
    }
  }

  function updateElement(target)
  {
    if (http_request.readyState == 4)
    {
      if (debug == true)
      {
        alert(http_request.responseText);
      }
      if (http_request.status == 200)
      {
        document.getElementById(target).innerHTML =
          http_request.responseText;
      }
      else if (debug == false)
      {
        alert('The server returned an error. Please set debug = true to see the full server response.');
      }
    }
  }