AJAX POST 请求仅在 Safari 5 中工作一次 [英] AJAX POST Request Only Works Once in Safari 5

查看:27
本文介绍了AJAX POST 请求仅在 Safari 5 中工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自己的自定义 AJAX 库(我对使用 jQuery 等不感兴趣),它在以下浏览器中完美运行:

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

  • 火狐 7
  • Chrome 14
  • IE 8
  • IE 8(兼容模式)

在上述浏览器中使用我的自定义 AJAX 库,我可以使用 GET 和/或 POST 方法以任意顺序发出任意数量的 AJAX 请求,并且它们都可以完美地工作.由于为每个请求创建了一个新的 AJAX 对象(参见下面的代码),我什至可以同时成功处理多个 AJAX 请求.

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.

但是,在 Safari 5 中,如果 AJAX POST 请求绝对是第一个要执行的 AJAX 请求,则它只会将 POST 数据传递给服务器.即使我连续两次执行完全相同的 AJAX POST 请求,POST 数据也只会在第一次请求期间传递给服务器.这是我的自定义 AJAX 库中的 JavaScript:

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);
    }
}

因此,例如,以下任何一个 AJAX POST 请求,如果它们是绝对第一个 AJAX 请求(无论是 GET 还是 POST),都将传递 POST 数据;否则,POST 数据不会被传递:

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");

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

我在整个 AJAX 库中都添加了调试语句,并且在每个 POST 请求之前按照预期在参数"变量中创建了 POST 参数.我完全不知道为什么,只有在 Safari 5(在提到的浏览器中),我有这个问题.有什么想法吗?

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?

提前致谢!杰西

推荐答案

调用失败的原因是在 IIS 下使用 Windows 身份验证时 Safari 中的错误.转到您网站的身份验证设置.右键单击 Windows 身份验证,选择提供程序并删除协商,让 NTLM 正常工作.我还没有测试过 Kerberos.

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.

此问题仅出现在某些版本的 safari 中.

This issue only appears in certain builds of safari.

这篇关于AJAX POST 请求仅在 Safari 5 中工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆