Page 1 of 1

IE AJAX bug - SOLVED

Posted: Tue Dec 05, 2006 6:41 pm
by monkeyfoot
Hi all! I'm trying to make a request to the server using AJAX. It works fine in Firefox but fails in IE. The only difference I have been able to determine is that Firefox is ok with the page extension as ".php" but IE is not. I replaced the file I was calling with a file with identical output but with an extension of ".xml" and IE took it and was happy as a clam. Has anyone run into this before? Below is my code:

//the original link that makes the request

Code: Select all

  <a href="javascript:void(0);"   onclick="sendAjaxRequest('getSigDef.php','id=1&sigid=1','outputSomething');">
       Click here to do something
</a>

//sending the request

Code: Select all

   
 function sendAjaxRequest(url,params,functionName) 
		{
	        var http_request = getObject();
	        if (!http_request) 
	        	{
	            	return false;
	        	}
	        else
	        	{
	        		http_request.onreadystatechange = function() 
	        			{
	        				eval(functionName)(http_request);
						};
					
	        	       http_request.open('POST', url, true);
			       http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			       http_request.setRequestHeader("Content-Length",params.length); 
			       http_request.send(params);  
	        	}  
	    }
//the function outputSomething()

Code: Select all

function outputSomething(http_request)
		{
			var ready = http_request.readyState;
			var data = null;
			var outputSnippet = "";
			if(ready == 4)
				{
					if (http_request.responseText.indexOf('invalid') == -1)
						{
							var myXML = http_request.responseXML;
					         	var sigDef = myXML.getElementsByTagName('sig');
							
                                                          if(sigDef.length >0) //this is where it dies.
                                                            //firefox returns 1, IE always returns 0
								{	
									for (t=1;t<6;t++)
										{
											//load stuff into an array
										}
									for (t=0;t<7;t++)
										{
											//load stuff into an array
										}
								}
							else
								{	
					       			alert("no nodes found");
								}
						}
				}
			else
				{
					//waiting...				
				}
		}     

I'm totally stumped. getSigDef.php fails while sigDef.xml works perfectly.

Here's the XML I'm using

Code: Select all

<?xml version="1.0"?>
<def>
<sig>
<p1name>A</p1name>
<p2name>B</p2name>
<p3name>C</p3name>
<p4name>D</p4name>
<p5name>E</p5name>
<p1Qty>0</p1Qty>
<p2Qty>0</p2Qty>
<p3Qty>0</p3Qty>
<p4Qty>0</p4Qty>
<p5Qty>0</p5Qty>
<d0>0</d0>
<d1>0</d1>
<d2>0</d2>
<d3>0</d3>
<d4>0</d4>
<d5>0</d5>
<d6>0</d6>
<withorwithout>0</withorwithout>
</sig>
</def>
Thanks for any and all help!

Posted: Tue Dec 05, 2006 10:47 pm
by Burrito
show us the getObject() function.

are you sure the php is actually outputting anything? Ajax only looks at info that is being 'echoed' to the client.

Posted: Wed Dec 06, 2006 7:05 am
by monkeyfoot
Burrito, here's the getObject() function. Yes, the php is definitely outputting the XML. I can take the parameters and put them in the querystring of the page and the XML returns correctly. (my page accepts supports GETs for testing purposes)

Code: Select all

function getObject()//creates the XMLHttpRequest Object
		{
			var http_request = false;
	        if (window.XMLHttpRequest)  // for Gecko Browsers
	            { 
	            	http_request = new XMLHttpRequest();
	            	if (http_request.overrideMimeType) 
	            		{
	                		http_request.overrideMimeType('text/xml');
				         }
	        	} 
	        else if (window.ActiveXObject) 
	        	{ // IE
	            	try 
	            		{
	                		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            		} 
	            	catch (e) 
	            		{
	                		try 
	                			{
	                    			http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                			}
	                		catch (e) 
	                		{
	                			alert("could not create an XMLHttpRequest Object!");
	                		}
	            		}
	        	}
	        return http_request;
		}

Posted: Wed Dec 06, 2006 8:27 am
by Burrito
weird. You might try escape() around your query string.

Posted: Wed Dec 06, 2006 8:49 am
by monkeyfoot
the thing is, when i do

Code: Select all

alert(http_request.responseText.length);
alert(http_request.responseText);
I get back 361 for the length and the XML as a string which I get in both FF and IE.

As far as I can tell (after several hours more of playing around with it) there seems to be an issue with the way apache sends static XML vs php-rendered XML to the browser.

I did the following to come to that conclusion:
added the following line in my httpd.conf file

Code: Select all

AddType application/x-httpd-php .xml
this would let php code execute in a .xml file. This then broke all the static XML i had, which was working (as i had mentioned above).
I then removed the line and my static XML worked again.

Does anyone know about how apache outputs data to the browser based on file type or whats being done in the code?

Posted: Wed Dec 06, 2006 9:02 am
by nickvd
Have you tried it in both IE6 and IE7?

IE7 is a little more strict about mimetypes and document types, so if the server is not sending a mimetype of application/xml (not sure if that's the right one) then ie wont read it as being xml and therefore you wont have access to it's dom...

Posted: Wed Dec 06, 2006 9:23 am
by redmonkey
Have you tried adding...

Code: Select all

header('Content-type: text/xml');
....to getSigDef.php? You might also need to add the charset attribute.

Posted: Wed Dec 06, 2006 9:45 am
by monkeyfoot
Thanks redmonkey, that helped me solve my problem. In getSigDef.php i was using an include to change the content-type. I was changing the type to

Code: Select all

header("Content-Type: application/xhtml+xml; charset=utf-8");
Since only mozilla supports that particular content type, IE was seeing it as

Code: Select all

header("Content-Type: text/html; charset=utf-8");
Thanks for everyones help!