AJAX not working in IE8

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

AJAX not working in IE8

Post by oscardog »

Hello,

The code works fine in Firefox and other modern browsers (even IE7 and IE9) but for some reason IE8 it just doesn't work.

I have used some alerts to try find the problem and as far as I can tell it setups the ajaxRequest / XMLHTTPRequest fine, however when I put an alert in the readyState 1 / 2 / 3 part nothing comes up so it looks like the script, for some reason, isn't sending the request.

This is the code

Code: Select all

function switchProduct(id) {
	
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	
 try
 {
  var ajaxRequest = new XMLHttpRequest();
 }

 catch(err1)
 {
  //For older browsers (IE6 etc)
  var ieXmlHttpVersions = new Array();
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

  var i;
  for (i=0; i < ieXmlHttpVersions.length; i++)
  {
   try
   {
    var ajaxRequest = new ActiveXObject(ieXmlHttpVersions[i]);
    //document.getElementById("Content").innerHTML="<h1>Using " + ieXmlHttpVersions[i] + "</h1>";
	
    break;
   }
   catch (err2)
   {
    
   }
  }
 }

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		var searchResponse = ajaxRequest.responseText;
		
		if(searchResponse != "") {
			splitResponse = searchResponse.split("---");
			
			$('#prod_detail_box').fadeOut('slow', function() {
				document.getElementById("prod_detail_box").style.backgroundImage = "none";
    			document.getElementById("prod_detail_box").innerHTML = '<a href="ecom-prodshow/' + splitResponse[1] + '.html" title="' + splitResponse[2] + ' - ' + splitResponse[0] + '"><img src="media/ecom/prodlg/' + splitResponse[3] + '" alt="' + splitResponse[2] + ' - ' + splitResponse[0] + '"></a><p class="aller">' + splitResponse[2] + '</p><p><a href="ecom-prodshow/' + splitResponse[1] + '.html">Full Specification</a>';
				$("#prod_detail_box").fadeIn('slow');
  			});

			
			
		} 
		
	} else if(ajaxRequest.readyState == 1 ||ajaxRequest.readyState == 2 || ajaxRequest.readyState == 3) {
		document.getElementById("prod_detail_box").style.backgroundImage = "url(media/loading.gif)";
	}
}

var url = "grab_prod.php";
var params = "prodid=" + id;
ajaxRequest.abort();
ajaxRequest.open("POST",url,true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(params);

}
Any ideas?

Thanks!
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: AJAX not working in IE8

Post by greyhoundcode »

Does JQuery's ajax implementation work with IE8?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: AJAX not working in IE8

Post by Jonah Bron »

It's my understanding that Internet Explorer has supported the XMLHttpRequest object since version 7.
innovativeinfosln
Forum Newbie
Posts: 2
Joined: Sun Dec 19, 2010 9:51 pm

Re: AJAX not working in IE8

Post by innovativeinfosln »

Quite possible what you think can be right .
but i would suggest that there can be an Error is invalid property value.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: AJAX not working in IE8

Post by Darhazer »

This is a scope resolution issue in the code.
Remove the unnecessary 'var' in xmlhttp initialization - only the first var ajaxRequest should be with var, as it introduces the variable in the function scope. The next assignments to ajaxRequest should be without var, so they remain in the scope of the whole function, and not in the loop scope for example.
Post Reply