Jump to content


Photo

Ajax problems...only in FF, IE works!


  • Please log in to reply
2 replies to this topic

#1 CrazyMerlin

CrazyMerlin
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 30 October 2006 - 11:46 PM

Hey guys,

Well I finally succumb to the fact that I had to use ajax.

I wrote a script that in IE works like a dream and is very fast. In firefox however , the readystate never seems to change to 4!

The data I was trying to get was http://70.87.49.164/...urveyData&sid=2
which as you can see, even in FF returns just fine.

And the client and server files are both on the same machine!

So how come my damn script works in IE but not FF.

Any help greatly appreciated.


	function doSurveySelect(selector, site_url)
	{
		survey_idx = selector[selector.selectedIndex].value;
		xmlHttp = createXMLHttpRequest();
		
		var url = site_url + 'rpc.php?cmd=getSurveyData&sid=' + survey_idx;
		
		xmlHttp.open('GET', url, true);
		xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.responseXML.childNodes.length > 0)
			{
				surveyDataToForm();
			}
		}
		
		try
		{
			xmlHttp.send(null);
		}
		catch(e)
		{
			return throwError('surv_form', e.description);
		}
		
		return;
	}
	
	function surveyDataToForm()
	{
		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
			.
			.
			.
			.
		}
		else
		{
			alert("Document not ready!");
		}
	}

	function createXMLHttpRequest()
	{
		if(window.XMLHttpRequest)
		{
			return new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			return new ActiveXObject("Msxml2.XMLHTTP");
			if(!xmlHttp)
			{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}

I know I should be checking for xmlHttp.readystate == 4, but when I do it never ever changes in IE or FF and so I get nothing...at least this way it works in IE.

Thx!

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 31 October 2006 - 02:48 AM

I don't have time to go picking through it, but I'd imagine it's because of how you're creating the object.  Here's some of my code, feel free to pick through it:

function createRequestObject()
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) 
			xmlhttp.overrideMimeType('text/xml');
    } 
	else if (window.ActiveXObject) 
	{ // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	return xmlhttp;
}

/* You can get more specific with version information by using parseInt(navigator.appVersion) Which will extract an integer value containing the version of the browser being used.  The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

function getNotifications()
{
	var uname = document.getElementById('user').value;
	var engine = document.getElementById('notify').options[document.getElementById('notify').selectedIndex].value;
	http.open('post', 'notifyupdate.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send('user='+uname+'&action=insert&notify='+engine);
	http.onreadystatechange = handleNotes; 
}

function removeNotifications(_boxid)
{
	http.open('post', 'notifyupdate.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send('user='+document.nform.user.value+'&action=delete&id='+_boxid);
	http.onreadystatechange = handleNotes; 
}

function handleNotes()
{
	if(http.readyState < 4) // loading the response
		document.getElementById('notes').innerHTML = "Loading...";
	
	if(http.readyState == 4) //Finished loading the response
	{
		var response = http.responseText;
		document.getElementById('notes').innerHTML = response;
	}
}

Info: PHP Manual


#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 31 October 2006 - 02:49 AM

By the way, mine works in Opera, FF, and IE for sure.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users