jQuery.load(),混合 HTTP/HTTPS 和 Internet Explorer
我正在尝试使用 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 确实如此。
I'm attempting to load in a remote HTML page using jQuery.load('https://someurl.com .someClass')
. The page doing the load is on HTTPS; the remote page is available as both HTTP and HTTPS. Everything works fine in reasonable browsers, but IE is throwing up a mixed HTTP/HTTPS content security warning -- the remote page has CSS and JS included by HTTP links, even when requested as HTTPS. Any clues as how to successfully pull in a mixed-content file in IE without it triggering the warning? Modifying the remote page isn't an option.
EDIT
To be clear, I'm attempting to load the remote file over HTTPS. The file contains links to HTTP resources (img, css, js); because I'm providing a selector to .load()
, reasonable browsers don't try to parse & execute the document; IE does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法绕过 IE 中的混合内容警告。如果远程资源可通过 HTTP 和 HTTPS 访问,您可以确保您的协议匹配
jQuery.load(location.protocol + '//someurl.com .someClass')
根据混合问题进行更新- 远程页面中的内容:
jQuery.load
将整个responseText加载到documentFragment中,然后拉出选择器指示的适当部分(请参阅jQuery 1.4.4 ajax.js)。整个远程页面被解析为 HTML,并且必须经过浏览器的安全流程;在许多方面,通过确保所有协议匹配和/或仅返回片段(如果这就是您所需要的),可以更简单地确保响应是“干净的”。如果您不修改其他资源(这将更加健壮),则需要将所有出现的 HTTP 替换为 HTTPS(反之亦然),而远程资源仍然只是一个细绳。下面是一个脆弱的 jQuery 插件作为该技术的示例,大部分摘自 jQuery 1.4.4 $.load 函数:
用法:
$('#your > .selector').protocolModifyLoad(location.protocol + '//someurl.com', 'thisIsFragile!! !', '.someClass');
此函数省略了
$.load
的callback
和params
参数以及添加 >yesIKnowThisIsFragile
参数作为微妙的提醒。You can't get around the mixed-content warning in IE. If the remote resource is available via both HTTP and HTTPS you can make sure your protocols match
jQuery.load(location.protocol + '//someurl.com .someClass')
Updated based on the problem being mixed-content in the remote page:
jQuery.load
loads the entire responseText into a documentFragment before pulling out the appropriate part indicated by the selector (see jQuery 1.4.4 ajax.js). The entire remote page is parsed as HTML and must go through the browser's security processes; in many ways it's simpler to make sure the response is "clean" by making sure all the protocols match and/or only returning a fragment if that's all you need.If you won't be modifying the other resource, which would be much more robust, you'll need to replace all occurrences of HTTP with HTTPS (or vice versa) while the remote resource is still just a string. Here's a fragile jQuery plugin as an example of this technique, mostly ripped from jQuery 1.4.4 $.load function:
Usage:
$('#your > .selector').protocolModifyLoad(location.protocol + '//someurl.com', 'thisIsFragile!!!', '.someClass');
This function omits the
callback
andparams
arguments of$.load
and theyesIKnowThisIsFragile
argument was added as a subtle reminder.如果安全页面加载任何不安全资源,它将引发警告。解决这个问题的唯一方法是从 https 加载所有内容。
即使其他浏览器也应该在某处显示警告(可能在 FF 中地址的左侧?)
If a secure page loads any nonsecure resource, its going to throw the warning. The only way to get around it is to load everything from https.
Even the other browsers should have a warning being displayed somewhere (possibly to the left of the address in FF?)