Jump to content

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


CrazyMerlin

Recommended Posts

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/rpc.php?cmd=getSurveyData&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.

[code]

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");
}
}
}
[/code]

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!
Link to comment
Share on other sites

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:

[code]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;
}
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.