Firefox 23和IE10不再对我的XMLHttpRequest友好,我在php中有了CORS

Firefox 23 and IE10 no longer playing nice with my XMLHttpRequest, I have CORS in php

本文关键字:php 我在 CORS 友好 XMLHttpRequest IE10 不再 我的 Firefox      更新时间:2023-09-26

我有一个java脚本,它只是从网站上获取一个ID,并将其传递给另一个单独域上的php脚本:

javascript打开:

https://myspot.com

并将进入

http://theotherwebsite.edu

这或多或少是可行的,我不得不使用以前的跨站点解决方案,但现在在firefox23和IE10上似乎没有得到认可。

以前的解决方案是使用这样的东西:

 var isIE10 = false; //this is beacuse stupid IE10 now does not work with the window.XDomainRequest
 /*@cc_on
     if (/^10/.test(@_jscript_version)) {
         isIE10 = true;
     }
 @*/
 console.log(isIE10);
 var isIE8 = window.XDomainRequest ? true : false; 
 var invocation=createCrossDomainRequest();
function createCrossDomainRequest(url, handler)
{
        var request;
        if ((isIE8) && (!isIE10)) //tried to hack my own isIE10 fix didnt work
        {
            request = new window.XDomainRequest();
        }
        else
        {
            request = new XMLHttpRequest();
        }
        return request;
}
 function callOtherDomain()
    {
        if (invocation)
        {
            if("withCredentials" in invocation) //was taking a stab in the dark with this.
            {
                invocation.onload=outputResult;
                invocation.open("GET", url, true);
                invocation.send();
            }
            else if(isIE8)
            {
                invocation.onload = outputResult;
                invocation.open("GET", url, true);
                invocation.send();
            }
            else
            {
                invocation.open('GET', url, true);
                invocation.onreadystatechange = handler;
                invocation.send();
            }
        }
        else
        {
            var text = "No Invocation TookPlace At All";
            var textNode = document.createTextNode(text);
            var textDiv = document.getElementById("textDiv");
            textDiv.appendChild(textNode);
        }
    }
function handler(evtXHR)
    {
        if (invocation.readyState == 4)
        {
            if (invocation.status == 200)
            {
                outputResult();                 
            }
            else
            {
                alert("Invocation Errors Occured " + invocation.status + " state: " + invocation.readyState);
            }
        }
    }
    function outputResult()
    {
          var response = invocation.responseText;

                    //get JSON of response
        var obj = JSON.parse(response);
        var mtype = obj.messagetype;
        var output = obj.message;
        var url = obj.url;          
        if(mtype=="error")
        {               
            parent.location=url;
        }
        else if(mtype=="warning")
        {
            var answer=confirm(output);
            if(answer)
                parent.location=url;
        }

        //var textDiv = document.getElementById("textDiv");
        //textDiv.innerHTML += response;
    }       

callOtherDomain();所以我不确定这里发生了什么,我在firefox 23上看到控制台中的一个错误:

Blocked loading mixed active content "http://theotherwebsite.edu"

我知道这是因为主脚本是在https上加载的,而不是http。但它以前并不关心。我也知道这个错误,在firefox的地址栏中设置了一个屏蔽,用户可以告诉它启用被阻止的内容。这对我来说不是一个可以接受的解决方案。如果我把我愚蠢的小php脚本放在https下,那也是我需要的证书吗?

然后IE10就不起作用了:

SCRIPT5: Access is denied.landing, line 64 character 421

因此,我不确定我需要做些什么才能让我的代码重新工作,让用户调整浏览器是不可行的,因为这是分布式企业范围的,这是为了让nag屏幕让他们知道根据php文件使用从网站通过ajax传来的ID访问的一些ldap条目来更改密码。

我在谷歌上搜索了一下,但一无所获,我发现的最多的是使网站兼容的php句柄:

<?php
 header('Access-Control-Allow-Origin: *');

这也是我最初实施的。所以不确定该尝试什么或者下一步该去哪里?这是一个简单的JSON字符串,我可以试试这里描述的飞行前方法吗:

http://ppe.blogs.msdn.com/b/ie/archive/2012/02/09/cors-for-xhr-in-ie10.aspx

如果我这样做了,我不确定标题应该是什么样子。

我本来打算发布firefox23响应标头,但它从未发出请求,因为它直接阻止了加载混合的活动内容。所以我想我有两个问题需要解决,一个是javascript生活在https上,并调用http。。。这可能是我在firefox中唯一的问题,不能100%确定我是否会有跨站点的问题。

IE10网络请求标头永远找不到,我正在IE10中的F12按键区域内查看,在网络下,我单击开始捕获,然后用xhr调用加载页面。

所以我想我在问firefox23和IE10发生了什么变化,让我的代码不再工作了?

Firefox 23+将阻止他们所称的"活动混合内容"。即:从安全网页(https)请求的非安全(http)位置托管的内容。在这种情况下,"活动"本质上意味着所有不是媒体类型的东西(不是图像、音频或视频资源)。这是为了防止使用非安全子请求进入安全页面的中间人攻击。

有关更多信息,请参阅MDN上的混合内容文章。

由于请求甚至在到达网络之前就被阻止了,因此不会有任何响应头/数据。

不确定IE10,但他们的文件似乎表明,他们出于同样的原因阻止了此类请求,称:

不允许跨域、跨端口和混合协议请求