ajax xmlhttp request object

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

ajax xmlhttp request object

Post 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]
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

try adobe labs' SPRY FRame work
it shud be easier to work and understand
http://labs.adobe.com
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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
}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply