I am using XMLHttpRequest as a simple one way ajax logging function. No server response is needed. The request does not cross domains.

This code works as expected in IE and in Firefox

var client2 = new XMLHttpRequest();
client2.open("GET", "program?url=c",true);
client2.send();
alert('sent');
return true;

however, as soon as I remove the alert() it fails in Firefox

var client2 = new XMLHttpRequest();
client2.open("GET", "program?url=c",true);
client2.send();
return true;

the server never receives the call...

When I step thru this in firebug, I can remove the alert line and once I step thru to the return line the function works as expected. But as soon as I eliminate the debugger (no break) it ceases to work again? very strange. no errors thrown.

I have tested this on crossbrower testing site live and determined that it is not a problem localized to my machine. The code works as expected in IE but requires the alert() to be fired in Firefox.

I tried this code with jquery.ajax with same results... in firefox, the request only works if the alert() line is present. I removed firebug, no luck. Tried corssbrowser testing again, same results...

I'm stumped... :(

update:

fascinating... in every debugger using step breakpoints the function works as expected until the breakpoints are removed, then the problem returns..

I tried defined a handler... if the handler invokes an alert() it works, no alerts, no go...

BUT:

setting the async to false fixes the problem... but I don't want a synchronous event... so what could the culprit be?

I haven't used raw XMLHttpRequest in a while - just so easy with jQuery. However, from what I remember don't you have to check ready states? I think maybe that your return is firing before you have actually caught a response. Your alert simply stops time allowing you to catch a response.

I could, and probably am, completely way off though :D

Member Avatar for diafol

I'm thinking along the same lines. Ajax is asynchronous, so the return is happening before the repsonse. The alert / breakpoint gives it time to respond. Using jQ - are you using .done()? If you call the rest of your actions from here, you should find that code execution has to wait until there's a response.

I think paulkd and diafol are on the right track in their suggestions. Your sample javascript is missing some of hte basic components in an ajax block.

Whenever I use javascript for a simple ajax call, i put my ajax code within a function and it would look something like this...

var xhr = false;
    if (window.XMLHttpRequest) {
        xhr = new window.XMLHttpRequest;
        }
        else {
              try {
                    xhr =  ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (ex) {
                    window.console.log("Error handling here...");
                }
            } 
        if (xhr) {
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("elementID").innerHTML = xhr.responseText;
                }
            }
            xhr.open("GET", "targetPage.ext", true);
            xhr.send(null);
        }

Source: Ajax XMLHttpRequest Object

I would agree with you that timing seems to be an issue. Changing the async flag to false solves the problem apparently. (There were some random issues so I can't say that 100% definitvely yet).

However, it is confusing to me that the code runs fine in IE. Since it is a simple message to the server I dont' see how this should matter. Note that it is not the response timing that is an issue because the server never receives the request (and so therefore no reply). The send() fails for some reason with no exception or error thrown that I can find.

The logs to the server are busy but I have done several tests that indicate that the request is never received at all in these instances.

The above example assumes a need for a response. The IE code is probably a good idea. I had left it out for now because the problem is w/ Firefox, and not IE. The code runs fine in IE.I experimented with adding a readystatechange hook even tho a reply is not needed but saw no difference.

If the problem persists I will try and craft a separate page for testing that I am able to share. Thank you for taking the time to respond. :)

The only thing I can think of that would explain this, is that right after the send your functions returns. This means client2 is out of scope and will be discarded. It's possible IE knows a request is running and waits, while FF immediately destroys the object, even before the actual data is sent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.