jQuery AJAX Oddities in Firefox

Ran into a strange problem today.

I’m building a site in PHP5 with some recently found Ajax stuff using the wonderful jQuery library. However, the problem I found was that when I used SSPI/NTLM authentication, the ajax queries sent to the server would always generate a login screen. It was very weird, because it worked fine in IE7, it worked fine whenever I ran normal load page requests from the browser itself, but whenever jQuery asked an ajax question, pop! came the login dialog again.

After some research, it turns out it wasn’t Firefox’s fault after all. Apparently, jQuery adds a “Connection: close” parameter in some specific cases (which happens to be every case on my machine), and that wrecked wreaked havoc with the NTLM authentication, because it happens in multiple stages over a single tcp connection, and closing the connection after every stage is a very bad idea.

Digging through the code I found the two lines

    if (xml.overrideMimeType)
        xml.setRequestHeader("Connection", "close");

which were the culprit. Since I didn’t want to hack jquery.js itself, causing a deployment/maintenance problem, I eventually found how to do it.

<script language="JavaScript">
    $.ajaxSetup( {
        beforeSend: function(xml) {
            if (xml.overrideMimeType)
                xml.setRequestHeader("Connection", "keep-alive");
        }
    } );
</script>

Case closed.

2 Comments

  1. Albis
    Posted April 24, 2007 at 17:35 | Permalink

    Wreaked havoc, not wrecked. (Well, at least, if quick googling is to be trusted, I win, you lose. Muhahähä!)

  2. Bart
    Posted February 18, 2008 at 15:02 | Permalink

    Thanks ! That did the trick for me..

Post a Comment

Your email is never shared. Required fields are marked *

*
*