AJAX - Get the ready state handler and request object

by Svelmoe 5. August 2007 17:17

Used to get the ready state handler for the AJAX call:

 function getReadyStateHandler(xmlObject, responseXmlHandler)
{
    return function ()
    {
        // If the request's status is "complete"
        if (xmlObject.readyState == 4)
        {
            // Check that a successful server response was received
            if (xmlObject.status == 200)
            { responseXmlHandler(xmlObject); } 
            else
            {
                // An HTTP problem has occurred
                alert("HTTP error: "+xmlObject.status);
            }
        }
    }
}

Used to create the xml request object for AJAX calls:

function getXMLHttpRequest()
{
  var xmlObject = false;
 
  if (window.XMLHttpRequest)
  {
    //Create non-Microsoft
    xmlObject = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    //Create ActiveX, first for modern browsers
    try
    { xmlObject = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (ex1)
    {
      //Older MS-Browser compability
      try
      { xmlObject = new ActiveXObject("Microsoft.XMLHTTP"); }     
      catch (ex2)
      {
        /*Could not create ActiveX at all.*/
        return false;
      }
    }
  }
  return xmlObject;
}

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



About Svelmoe

My real name is Allan Svelmøe Hansen.

I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004. Svelmoe.dk is a place for my every day thoughts and reactions and the occasional technical blog entry.

I also blog about SQL and MS SQL Server at www.execsql.com so in case you are looking for more about that, please visit that website.



View Allan Svelmøe Hansen's profile on LinkedIn     

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Svelmoe.dk