AJAX POST请求,只有一次在Safari 5 [英] AJAX POST Request Only Works Once in Safari 5

查看:204
本文介绍了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库,我可以,因为我想使尽可能多的AJAX请求,以任何顺序,使用GET和/或POST方法,他们都工作得很好。由于新的AJAX对象为每个请求创建(见下文code),我甚至可以同时有多个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请求只传递POST数据到服务器,如果它是绝对的第一AJAX请求执行。即使我连续执行完全相同的AJAX POST请求两次,POST数据只传递到服务器的第一请求中。这里是JavaScript在我的自定义AJAX库:

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请求将通过POST数据,如果它们是绝对第一AJAX请求(是否GET或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天全站免登陆