internet-explorer - jQuery.load(),混合 HTTP/HTTPS 和 Internet Explorer

标签 internet-explorer jquery https

我正在尝试使用 jQuery.load('https://someurl.com .someClass') 加载远程 HTML 页面。执行加载的页面位于 HTTPS 上;远程页面可用作 HTTP 和 HTTPS。在合理的浏览器中一切正常,但 IE 抛出混合 HTTP/HTTPS 内容安全警告 - 远程页面包含 HTTP 链接中包含的 CSS 和 JS,即使作为 HTTPS 请求也是如此。关于如何在 IE 中成功提取混合内容文件而不触发警告的任何线索?修改远程页面不是一个选项。

编辑

需要明确的是,我正在尝试通过 HTTPS 加载远程文件。该文件包含 HTTP 资源(img、css、js)的链接;因为我为 .load() 提供了一个选择器,所以合理的浏览器不会尝试解析和执行文档; IE 确实如此。

最佳答案

您无法绕过 IE 中的混合内容警告。如果远程资源可通过 HTTP 和 HTTPS 访问,您可以确保您的协议(protocol)匹配 jQuery.load(location.protocol + '//someurl.com .someClass')


根据远程页面中混合内容的问题进行更新:

jQuery.load整个responseText加载到documentFragment中,然后拉出选择器指示的适当部分(请参阅 jQuery 1.4.4 ajax.js )。整个远程页面被解析为 HTML,并且必须经过浏览器的安全流程;在许多方面,通过确保所有协议(protocol)匹配和/或仅返回一个片段(如果这就是您所需要的),可以更简单地确保响应是“干净的”。

如果您不打算修改其他资源(这会更加健壮),则需要在远程资源处于可用状态时将所有出现的 HTTP 替换为 HTTPS(反之亦然)仍然只是一个字符串。这是一个脆弱的 jQuery 插件作为该技术的示例,大部分摘自 jQuery 1.4.4 $.load function :

(function($){
    var http = "http:",
        https = "https:",
        rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
        proto = location.protocol,
        otherProtoUri = new RegExp("\\b" + (proto === http ? https : http) + "(//[a-z\\d.-]+)", "gi");

    $.fn.protocolModifyLoad = function(url, yesIKnowThisIsFragile, selector) {
        var self = this;

        if ( !this.length || !yesIKnowThisIsFragile) {
            return this;
        }

        $.ajax({
            url: url,
            type: "GET",
            dataType: "html",
            complete: function( res, status ) {
                // If successful, inject the HTML into all the matched elements
                if ( status === "success" || status === "notmodified" ) {
                    // Force occurences of the other protocol into the current one
                    var response = res.responseText.replace(otherProtoUri, proto + "$1");

                    // See if a selector was specified
                    self.html( selector ?
                        // Create a dummy div to hold the results
                        jQuery("<div>")
                            // inject the contents of the document in, removing the scripts
                            // to avoid any 'Permission Denied' errors in IE
                            .append(response.replace(rscript, ""))

                            // Locate the specified elements
                            .find(selector) :

                        // If not, just inject the full result
                        response);
                }
            }
        });

        return this;
    };
})(jQuery);

用法:$('#your > .selector').protocolModifyLoad(location.protocol + '//someurl.com', 'thisIsFragile!!!', '.someClass');

此函数省略了 $.loadcallbackparams 参数,并且将 yesIKnowThisIsFragile 参数添加为微妙的提醒。

关于internet-explorer - jQuery.load(),混合 HTTP/HTTPS 和 Internet Explorer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4404715/

相关文章:

ssl - 如何从 Insomnia REST Client 创建 SSL key 日志?

IE 中的 Javascript 错误(翻转)

javascript - 跨浏览器开发

javascript - JS 不能在 IE 上运行

javascript - 如何将数据值设置为div下的div?

java - trustAnchors 参数必须非空

javascript - 检测 "File download"弹出窗口何时关闭

javascript - 如何在 html 表体中使用 jQuery 或 javascript?

javascript - 根据选定的值启用按钮

https - Guzzle 和HTTPS