Page 1 of 1

Ajax problem in firefox only -- any help?

Posted: Sun Oct 29, 2006 12:53 pm
by CrazyMerlin
Weirdan | 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: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys!

I am an experience developer (php5 and javascript) but I've just started to use Ajax and have come across an issue that only shows it's head in FF.

I have the following functions in a js file:
[syntax="javascript"]
    var xmlHttp;

    function doSurveySelect(selector, site_url)
    {
		survey_idx = selector[selector.selectedIndex].value;
		createXMLHttpRequest();
		
		var url = site_url + 'rpc.php?cmd=getSurveyData&sid=' + survey_idx;
		xmlHttp.open('GET', url);
		xmlHttp.onreadystatechange = surveyDataToForm;
		
		try
		{
			xmlHttp.send(null);
		}
		catch(e)
		{
			return throwError('surv_form', e.description);
		}
		
		return;
	}
	
	function surveyDataToForm()
	{
		try
		{
			if (xmlHttp.readyState == 4) 
			{
				if(xmlHttp.status == 200)
				{
					//do some error checking/handling
					if(xmlHttp.responseXML.documentElement && xmlHttp.responseXML.documentElement.hasChildNodes)
					{
						if(xmlHttp.responseXML.documentElement.firstChild.nodeName == 'error')
						{
							var error_text = xmlHttp.responseXML.documentElement.firstChild.text.toString();
							error_text = "XML-RPC Error: " + error_text;
							return throwError('surv_form', error_text);
						}
					}
					else
					{
						var error_text = "No data returned from query!";
						error_text = "XML-RPC Error: " + error_text;
						return throwError('surv_form', error_text);
					}
					
					//populate form with gotten values
					.
					.
					.
					.
				}
			}
		}
		catch(e)
		{
			return throwError('surv_form', e.description);
		}

        function createXMLHttpRequest()
	{
		if(window.XMLHttpRequest)
		{
			xmlHttp = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			if(!xmlHttp)
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}
The url that gets passed is : http://70.87.49.164/rpc.php?cmd=getSurveyData&sid=2
As you can see this does in fact return valid xml data and does so in FF.
So I cannot understand why my script is failing.

When I debug using Venkman, it seems like the xmlHttp.readystate is always 1 (loading) and never seems to get to 4.

As I said, this works fine in IE. If anyone thinks they can help and needs to see the application in action, let me know and I will create you a temporary account on the system.

I have bought an ajax book, and that has been no help as to me the code looks fine.

Thanks for any and all help/comments.


Weirdan | 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: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Oct 30, 2006 6:23 am
by kettle_drum
Have you got the live http headers extension installed or anything else that will let you see if your getting any data or response back or is the request just timing out?[/list]

Posted: Mon Oct 30, 2006 10:55 am
by CrazyMerlin
I checked the response using firebug, and the correct xml data is indeed being returned, but the readystate is never being changed and so onreadystatechange never gets fired.

This is very frustrating as it makes no sense at all.

If data is being returned, why on earth is the ready state perpetually 1 (loading)?

I have debugged everything I can with 2 debuggers Firebug and Venkman and both say that readystate never changes, although Firebugs examination of the xmlHttpRequest shows the GET returned with the correct data.

How can this be?

Posted: Mon Oct 30, 2006 11:05 am
by jmut
shot in the dark.
haven't seen your exact implementation.
just make sure you have
Cache-Control: no-cache
header or/and the like on your php side.
otherwise same data might be returned.

Posted: Mon Oct 30, 2006 4:24 pm
by CrazyMerlin
ok, I amended my code slightly after spending the entire day looking around on the new, all to no avail really:

this stopped the variable from being global so it was easier to debug in firebug.

Code: Select all

function doSurveySelect(selector, site_url)
	{
		var survey_idx = selector[selector.selectedIndex].value;
		var xmlHttp = createXMLHttpRequest();
		var url = site_url + 'rpc.php?cmd=getSurveyData&sid=' + survey_idx;
		
		xmlHttp.open('GET', url);
		xmlHttp.onreadystatechange = function(){ surveyDataToForm(xmlHttp) };
		xmlHttp.send(null);
	}
Using firebug I kept checking the readystate after each executed line, and the readystate only changes once all execution is done and control goes back to the page. But then the surveyDataToForm() method never gets fired.

It is crazy!

Ajax just does not work for me in FF! I am quite sure now after reading 2 books and about 400 posts that there is nothing at all wrong in my code, Ajax is simply not doing what it is being told to do.

The wildest thing is, it works fine in IE! And even stranger....we use ajax on other parts of our site and that works fine!

Grrrrrr! Why did I ever even bother, I have just spent the entire day reading other peoples posts and nothing has helped at all.

Thanks for any comments....looks like I will have to restrict ajax to users with IE and refresh the page if they have FF....man, and they call this development!

@hmmm....how you get JS tags? I like that but couldn't see it!

Posted: Mon Oct 30, 2006 5:58 pm
by Weirdan
@hmmm....how you get JS tags? I like that but couldn't see it!

Code: Select all

[syntax="programming_language"]
code
[/syntax]
e.g:

Code: Select all

[syntax="javascript"]
function doSurveySelect(selector, site_url)
{
   var survey_idx = selector[selector.selectedIndex].value;
   var xmlHttp = createXMLHttpRequest();
   var url = site_url + 'rpc.php?cmd=getSurveyData&sid=' + survey_idx;
         
   xmlHttp.open('GET', url);
   xmlHttp.onreadystatechange = function(){ surveyDataToForm(xmlHttp) };
   xmlHttp.send(null);
}
[/syntax]

Firebug is enabling a Firefox bug with XMLHTTP requests

Posted: Wed Nov 08, 2006 7:06 pm
by raiteria
I think you should check this link from Josh H

http://www.joehewitt.com/software/firebug/faq.php

the section on XMLHttpRequests
unfortunately he doesn't provide a ton of details.

Posted: Thu Nov 09, 2006 3:05 am
by Maugrim_The_Reaper
I use Firebug and Firefox 2.0. I'm not showing any AJAX problems. Can you confirm your Firefox version and whether you are using synchronous or asynchronous requests?

Personally I stopped using custom XHR calls long ago (the number of try/catch blocks and the length was too much of a headache to debug on my own). Might be worth looking into a third party solution like Prototype, jQuery, etc.

firebug likely cause

Posted: Sun Nov 12, 2006 7:45 pm
by robertmo
I ran into this similar problem in my ff ( vsn. 1.5.0.8 ) - IE 6 OK.
Simple Ajax example with FF and Firebug on returned correct data but nothing happened. A previous post gave a link to possible causes and turning FB off (for working purposes) was a quick fix for me.

http://www.joehewitt.com/software/firebug/faq.php
Sometimes XMLHttpRequests don't work when I am using FireBug. Why not?
There are some conditions in which FireBug causes XMLHttpRequests to fail. Unfortunately, this is due to bugs in Firefox itself, and I have yet to discover a workaround. There are two circumstances in which your requests won't work while using FireBug:

1. When using synchronous XMLHttpRequests. That is, if you pass false as the third parameter to request.open.
2. If your HTML document is loaded as XML, otherwise known as XHTML.

Hope that helps!

Rob