Page 1 of 1

ajax xmlhttp request object

Posted: Mon Dec 18, 2006 9:12 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi,

i'm using the code below to make asynchronous ajax calls to a server. it works with firefox but on IE6 i get an alert pop-up saying 'no request object'. does any of you know a fix for this, or can point me to a better script? 

[syntax="javascript"]
function createXmlHttpRequestObject()
{
  var xmlHttp;   //stores ref to the XMLHttpRequest object
  
  /** for non IE6 or older browsers **/
  try {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e) {
    //assume IE6 or older
    var xmlHttpVersions = new array ("MSXML2.XMLHTTP.6.0",
	                                 "MSXML2.XMLHTTP.5.0",
	                                 "MSXML2.XMLHTTP.4.0",
	                                 "MSXML2.XMLHTTP.3.0",
	                                 "MSXML2.XMLHTTP",
									 "Microsoft.XMLHTTP");
    //try every prog until one works
	for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++) {
	 try {  
	   xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
	 }
	 catch (e) {}
    }
  }
  
  /*********** for all other browsers ****************/
  if (!xmlHttp) 
    alert('error creating the XMLHttpRequest object.');
  else 
    return xmlHttp;
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Dec 20, 2006 9:44 am
by kendall
try adobe labs' SPRY FRame work
it shud be easier to work and understand
http://labs.adobe.com

Posted: Wed Dec 20, 2006 12:43 pm
by AKA Panama Jack
This is what I use and it works in Opera, Firefox and IE.

Code: Select all

function makeRequest()
{
	if ( xmlDoc == null )
	{
		if (typeof window.ActiveXObject != 'undefined' ) {
			xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
			xmlDoc.onreadystatechange = process ;
		}
		else
		{
			xmlDoc = new XMLHttpRequest();
			xmlDoc.onload = process ;
		}
	}
	xmlDoc.open( "GET", "http://localhost", true );
	xmlDoc.send( null );
}

function process() {
	if ( xmlDoc.readyState != 4 ) return ;
	// process something if connection object is created
}

Posted: Thu Dec 21, 2006 2:51 am
by CoderGoblin
XMLHttp tutorial (who's online example) may be of use. You could also try (Shameless plug on my part) Dynamic/Chained Selects using Ajax with Prototype for an example using the prototype javascript library.