2

I use my own custom AJAX library (I'm not interested in using jQuery, etc.), which is working flawlessly in the following browsers:

  • Firefox 7
  • Chrome 14
  • IE 8
  • IE 8 (compatibility mode)

Using my custom AJAX library in the aforementioned browsers, I can make as many AJAX requests as I want, in any order, using GET and/or POST methods, and they all work flawlessly. Since a new AJAX object is created for every request (see code below), I can even have more than one AJAX request process simultaneously with success.

However, in Safari 5 an AJAX POST request only passes POST data to the server if it is the absolute first AJAX request to execute. Even if I execute the exact same AJAX POST request twice in a row, the POST data is only passed to the server during the first request. Here is the JavaScript in my custom AJAX library:

if (!Array.indexOf)
{
    Array.prototype.indexOf = function(obj) { for (var i = 0; i < this.length; i++) { if (this[i] == obj) { return i; } } return -1; };
}

function ajaxObject()
{
    if (window.ActiveXObject)
    {
        var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i = 0; i < activexmodes.length; i++)
        {
            try
            {
                return new ActiveXObject(activexmodes[i]);
            }
            catch (e)
            {

            }
        }
    }
    else if (window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    }
    else
    {
        return false;
    }
}

function ajaxRequest(aURI, aContainerId, aPostData, aResponseType, aAvoidBrowserCache)
{
    // Initialize
    var xmlhttp = new ajaxObject();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if (aResponseType != "eval" && aResponseType != "EVAL")
            {
                // Show HTML for response
                document.getElementById(aContainerId).innerHTML = xmlhttp.responseText;
            }
            else
            {
                // Parse & execute JavaScript for response
                var responseText = xmlhttp.responseText;
                var startPos, endPos;
                for (var i = 0; i < responseText.length; i++)
                {
                    if (responseText.substring(i, i + 6) == "<eval>")
                    {
                        startPos = i + 6;
                        break;
                    }
                }
                for (var i = startPos; i < responseText.length; i++)
                {
                    if (responseText.substring(i, i + 7) == "</eval>")
                    {
                        endPos = i;
                        break;
                    }
                }
                textToEval = responseText.substring(startPos, endPos);
                eval(textToEval);
            }
        }
        else
        {
            try
            {
                if (xmlhttp.status != 0 && xmlhttp.status != 200)
                {
                    alert('Error ' + xmlhttp.status);
                }
            }
            catch (e)
            {
                // Handle IE8 debug "unknown error"
            }
        }
    }
    if (aAvoidBrowserCache != false)
    {
        // Combat browser caching:
        aURI = aURI + (aURI.indexOf("?") == -1 ? "?" : "&");
        theTime = new Date().getTime();
        aURI = aURI + theTime + "=" + theTime;
    }
    // Make request
    if (typeof aPostData == "undefined" || aPostData == null || aPostData == "")
    {
        // GET request
        xmlhttp.open("GET", aURI, true);
        xmlhttp.send();
    }
    else
    {
        // POST request
        var parameters = "";
        if (aPostData.constructor.toString().indexOf("Array") != -1)
        {
            // Use parameters passed as array
            for (var postCount = 0; postCount < aPostData.length; postCount++)
            {
                if (parameters != "")
                {
                    parameters = parameters + "&";
                }
                parameters = parameters + aPostData[postCount][0] + "=" + encodeURIComponent(aPostData[postCount][1]);
            }
        }
        else
        {
            // Use parameters passed as string
            parameters = aPostData;
        }
        xmlhttp.open("POST", aURI, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(parameters);
    }
}

So for example, either of the following AJAX POST requests will pass POST data if they are the absolute first AJAX request (whether GET or POST); otherwise, the POST data is not passed:

ajaxRequest("test.aspx", "", [["name1","value1"],["name2","value2"]], "eval");

or

ajaxRequest("test.aspx", "", "name1=value1&name2=value2", "eval");

I have added debug statements all throughout my AJAX library, and the POST parameters are being created in the "parameters" variable as expected prior to each POST request. I have absolutely no idea why, only in Safari 5 (out of the mentioned browsers), I have this problem. Any ideas?

Thanks in advance! Jesse

Jesse
  • 608
  • 8
  • 19

3 Answers3

7

The reason the call is failing is because of a bug in Safari when working with Windows Authentication under IIS. Go to the Authentication settings of your website. Right click on Windows Authentication, choose providers and remove Negotiate, leaving NTLM which works fine. I haven't tested Kerberos.

This issue only appears in certain builds of safari.

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • 1
    This is a belated response, but Aran you were absolutely correct. However, for IIS 6, the instructions for removing Negotiate are a bit different, found here: http://msdn.microsoft.com/en-us/library/cc339532(v=vs.90).aspx. Thank you very much!!! – Jesse Sep 12 '12 at 18:06
0

Came here from the thread you mentioned might be a dupe. I never solved our problem, but have you tried a simple page making post requests? With our issue it's a post problem, not an AJAX problem, we're still stumped though.

What version of IIS are you running on the server?

Chris
  • 8,268
  • 3
  • 33
  • 46
  • Pages that have been on my company intranet for years that accept POST data from HTML forms work fine in Safari 5.x. However, use of my AJAX library never results in Safari 5.x handling POST requests/responses successfully. Your and Derek's comments have helped me narrow my problem down to a POST-related issue between IIS 5.x/6.x and Safari 5.x (and probably other older versions of these products also), since these versions are what I am dealing with in my environment. Thank you for your input! – Jesse Mar 14 '12 at 14:58
  • Always nice to find other people experiencing similar issues; since I was writing my own AJAX methods as a newcomer to understand the mechanics (and javascript), I was sure the issue was something I had caused. It took quite a while to strip down the problem to basics. Thanks for pointing me to this thread! – Chris Apr 16 '12 at 13:07
0

I can confirm that the problem seems related to some sort of interaction between Safari & IIS. Luckily, I only develop and test this portion of the code on Windows. I moved it unchanged to a LAMP (Linux/Apache) staging server (prior to moving to our LAMP production server) and the problem went away. I was seeing the problem with Safari 5, IIS 5.1, & an ActiveState Perl 5.6 CGI.

Under RHEL 5, Apache 2.2, & Perl 5.8, it is gone.

Derek P
  • 51
  • 5
  • Thank you and I apologize for the delayed response! I figured I would be notified via email if anyone responded to my inquiry; hence the delay. We are running IIS 5.x and 6.x in various environments, and since posting this thread I have modified my AJAX library and still found that Safari 5.x always has problems with POST requests to these servers, even in our production environment. I am glad you confirmed the problem went away on your LAMP server, so at least I can let my team know that it is an issue with the browser and IIS rather than the code itself. Much appreciated! – Jesse Mar 14 '12 at 14:38
  • P.S. I would vote you up but I don't have the reputation yet, sorry :) – Jesse Mar 14 '12 at 14:42