AJAX:301/302标头重定向(跨域)在某些Webkit浏览器中失败

时间:2013-04-03 06:53:07

标签: javascript redirect header webkit cross-domain

尝试将302从一台服务器重定向到另一台没有内容的服务器,仅用于跟踪目的,我担心会丢失一些使用Apple iOS设备和一些Webkit / Chrome浏览器的访问者。

HTTP/1.1 302 Moved Temporarily
Date: Tue, 26 Mar 2013 20:00:03 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.2.6
Expires: Tue, 03 Jul 2001 06:00:00 GMT
Last-Modified: Tue, 26 Mar 2013 20:00:03 GMT
Cache-Control: no-store
Pragma: no-cache
P3P: CP="STP CUR OUR"
x-own: 46.163.123.40
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Location: http://target2.com
Connection: close
Content-Type: text/html; charset=iso-8859-1

在Firefox和IE上,重定向没问题。从domainA到domainA的重定向也没问题。 Webkit / Chrome / Mobile Safari浏览器上的跨域重定向失败: - (

javascript代码段如下所示:

testRedirection=function(url)
{
    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();//ff,webkit
    else if (window.ActiveXObject)
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//ie
    if(xmlhttp)
    {
        xmlhttp.open("HEAD",url,true);//tried HEAD, GET, POST
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200 || (xmlhttp.status>200 && xmlhttp.status<400))
                    alert("arrived with status "+xmlhttp.status);
                else
                    alert("failed with status "+xmlhttp.status);
            };
        };
        xmlhttp.send();
    };
    //\
};

无论如何,在iPhone和iPad设备以及Chrome上,我记录了流量,并且它永远不会通过target2.com到达,停止时没有错误或警告。

仅在跨域标头重定向时才会发生这种情况。是否有额外的标题允许这个?

1 个答案:

答案 0 :(得分:0)

这是一项安全功能。就脚本而言(例如Javascript),该请求被认为是危险的并且被阻止。仅在> = 200 <范围内的响应。允许300人​​。