Solved

Ajax xmlHttpObject.open() call works in Mozilla but not in IE

Posted on 2009-07-15
5
807 Views
Last Modified: 2012-05-07
I have a piece of javascript code for Ajax requests that works great in Mozilla but not in IE.

The main method that will be called is getSpeed()
It seems the error in IE is at the point when I call: xmlHttpObject.open(....
The IE throws "Unknown runtime error"

I'm new at Ajax and not sure what I'm doing wrong. Can someone please advise?

Thanks in advance for all the help.
var xmlHttpObject = getXmlHttpRequestObject();
 

/**

 * Create XMLHttpRequestObject.

 **/

function getXmlHttpRequestObject()

{  

	var xmlHttp;

  	try

   	{    

		alert("// Firefox, Opera 8.0+, Safari");    

		xmlHttp=new XMLHttpRequest();    

	}

  	catch (e)

    	{	alert("// Internet Explorer");    

		try

      		{	      

			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      

		}

    		catch (e)

      		{      

			try

        		{        

				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch (e)

        		{        

				alert("Your browser does not support AJAX!");        

				return false;        

			} // end catch

		}   // end catch

	}  // end catch

	

  	alert("xmlHttp:" + xmlHttp);

  	

	return xmlHttp;

} // end function
 

/**

 * Called when the AJAX response is returned

 **/ 

function handleResponse() 

{	

	// Check to make sure our response is ready

	if (xmlHttpObject.readyState == 4) 

	{	

		//alert('responseText:' + xmlHttpObject.responseText);

		document.getElementById('speedResultTable').innerHTML=xmlHttpObject.responseText;

		

		//$('#speedResultTable').flexigrid({height:auto});

	} // end if

} // end function
 

/**

 * Get speed data.

 */

function getSpeed()

{

	alert("startTimeToBeSent: " + startTimeToBeSent + 

			", endTimeToBeSent: " + endTimeToBeSent + 

			", pairIdToBeSent: " + pairIdToBeSent

		);

	document.getElementById('speedResultTable').innerHTML='<tr><td><img src="img/ajax-loader.gif"></td></tr>';

	

	alert("xmlHttpObject:" + xmlHttpObject);

	

	xmlHttpObject.open("GET", 

			'getSpeed.jsp?startTime=' + startTimeToBeSent + 

			'&endTime=' + endTimeToBeSent + 

			'&pairing=' + pairIdToBeSent, true);

					

	

	alert("call handle response");

	

	// This line will be called when we receive our AJAX response.

	xmlHttpObject.onreadystatechange = handleResponse; 		
 

	xmlHttpObject.send(null);	

} // end function

Open in new window

0
Comment
Question by:rnicholus
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Hube02
ID: 24861491
Attached is the function that I use and I've not had it fail in any testing.
    function createXmlHttpRequestObject() {

      // create new XMLHttpRequest Object

      var xmlHttp == false;

      

      // will store the reference to the XMLHttpRequest object

      // this should work for all browsers except IE6 and older

      try {

        // try to create XMLHttpRequest object

        xmlHttp = new XMLHttpRequest();

      }

      catch(e) {

        // failed to create XMLHttpRequest object

        // 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 id until one works

        for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {

          try { 

            // try to create XMLHttpRequest object

            xmlHttp = new ActiveXObject(XmlHttpVersions[i]);

          } 

          catch (e) {}

          // failed to create object

          // do nothing and try the next

        }

      }

      // return the object

      // returns false if we were unable to create any object

      return xmlHttp;

    }

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 24862339
copy and paste the following:
var xmlHttpObject = getXmlHttpRequestObject();

 

/**

 * Create XMLHttpRequestObject.

 **/

function getXmlHttpRequestObject()

{  

        var xmlHttp=null;

        try

        {    

                alert("// Firefox, Opera 8.0+, Safari");    

                xmlHttp=new XMLHttpRequest();    

        }

        catch (e)

        {       alert("// Internet Explorer");    

                try

                {             

                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      

                }

                catch (e)

                {      

                        try

                        {        

                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

                        }

                        catch (e)

                        {        

                                alert("Your browser does not support AJAX!");        

                                return false;        

                        } // end catch

                }   // end catch

        }  // end catch

        

        alert("xmlHttp:" + xmlHttp);

        

        return xmlHttp;

} // end function

 

/**

 * Called when the AJAX response is returned

 **/ 

function handleResponse() 

{       

        // Check to make sure our response is ready

        if (xmlHttpObject.readyState == 4) 

        {       

                //alert('responseText:' + xmlHttpObject.responseText);

                document.getElementById('speedResultTable').innerHTML=xmlHttpObject.responseText;

                

                //$('#speedResultTable').flexigrid({height:auto});

        } // end if

} // end function

 

/**

 * Get speed data.

 */

function getSpeed()

{

        alert("startTimeToBeSent: " + startTimeToBeSent + 

                        ", endTimeToBeSent: " + endTimeToBeSent + 

                        ", pairIdToBeSent: " + pairIdToBeSent

                );

        document.getElementById('speedResultTable').innerHTML='<tr><td><img src="img/ajax-loader.gif"></td></tr>';

        

        alert("xmlHttpObject:" + xmlHttpObject);

        if(!xmlHttpObject)return false;

	   

        // This line will be called when we receive our AJAX response.

        xmlHttpObject.onreadystatechange = handleResponse;              
 

        xmlHttpObject.open("GET", 

                        'getSpeed.jsp?startTime=' + encodeURIComponent(startTimeToBeSent )+ 

                        '&endTime=' + encodeURIComponent(endTimeToBeSent) + 

                        '&pairing=' + encodeURIComponent(pairIdToBeSent, true));

                                        

        

        alert("call handle response");

        

 

        xmlHttpObject.send(null);       

} // end function

Open in new window

0
 

Author Comment

by:rnicholus
ID: 24871605
Here's what I found. It's very weird:

Keep testing for a while and then the "Unknown Runtime Error" happens here:
document.getElementById('speedResultTable').innerHTML='<tr><td><img src="img/ajax-loader.gif"></td></tr>';

Then I tried removing the "tr" and "td":
document.getElementById('speedResultTable').innerHTML='<img src="img/ajax-loader.gif">';
Suddenly the problem disappear.

I put the "tr" and "td" back just out of curiosity. There's no more problem.
I'm confused. =(

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 24872264
It's possible that the first time you are using document.getElementById('speedResultTable'), there is no DOM object for that table yet, in which case the error you are describing would occur. For example, if you make the ajax call NOW, and 3 seconds later you dynamically insert the table AND the ajax completes before you insert the table, that's a problem. Another possibility is if your page has NOT finished loading and you try to derefence the table before the table's definition -ex:

WRONG:

<script>

var x = document.getElementById('theTable')

</script>

<table id='theTable'>...</table>
 

OK

WRONG:

<table id='theTable'>...</table>

<script>

var x = document.getElementById('theTable')

</script>

Open in new window

0
 

Author Comment

by:rnicholus
ID: 24896657
hielo,
thanks for your explanation.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello World !, Thanks : My Sincere thanks to @Michael Male who encouraged me to write an article on this in my  blog (http://scribblejava.wordpress.com). Introduction:       There are several technical issues a developer may face in his…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now