在 http 页面上使用 https 的 Ajax
我的网站使用http和https协议; 它不影响内容。 我的网站使用 jQuery ajax 调用,它也填充了页面上的某些区域。
现在,我想通过 https 进行所有 ajax 调用。 (请不要问我为什么:)) 当我在使用 https 协议的页面上时,ajax 请求正在工作。 当我在使用 http 协议的页面上时,出现 javascript 错误: 访问受限制的 URI 被拒绝
我知道这是一个跨域问题(事实上,这是一个跨协议问题),并且我知道我应该在 ajax 调用中使用与当前页面相同的协议。
尽管如此,我还是希望所有 ajax 调用都是 https,并在通过 http 提供服务的页面上调用它们。 是否有任何解决方法可以实现此目的(某些 json/代理解决方案?),或者根本不可能?
My site uses http and https protocol; it doesn't affect the content. My site uses jQuery ajax calls, which fills some areas on the page, too.
Now, I would like to do all ajax calls over https. (please dont ask me why :))
When I am on a page with https protocol, ajax requests are working.
When I'm on a page with http protocol, I get a javascript error:
Access to restricted URI denied
I know that this is a cross domain problem (in fact, it's a cross protocol problem), and I know that I should use the same protocol in ajax calls as on the current page.
Still, I want to all ajax calls to be https, and call them on a page that was served over http.
Is there any workaround to achieve this (some json/proxy solution?), or is it simply impossible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从服务器添加 Access-Control-Allow-Origin 标头
http://en.wikipedia.org/维基/Cross-Origin_Resource_Sharing
Add the Access-Control-Allow-Origin header from the server
http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
尝试 JSONP。
大多数 JS 库使其与其他 AJAX 调用一样简单,但在内部使用 iframe 来执行查询。
如果您不使用 JSON 作为负载,那么您必须在 iframe 周围滚动您自己的机制。
就我个人而言,我只是从 http:// 页面重定向到 https:// 页面
Try JSONP.
most JS libraries make it just as easy as other AJAX calls, but internally use an iframe to do the query.
if you're not using JSON for your payload, then you'll have to roll your own mechanism around the iframe.
personally, i'd just redirect form the http:// page to the https:// one
http://example.com/ 可能会解析为与 https://example.com/ (由于未发送主机标头,因此响应该 IP 的默认值),因此两者被视为单独的域,因此受到跨域 JS 限制。
JSON 回调 可以让您避免这种情况。
http://example.com/ may resolve to a different VirtualHost than https://example.com/ (which, as the Host header is not sent, responds to the default for that IP), so the two are treated as separate domains and thus subject to crossdomain JS restrictions.
JSON callbacks may let you avoid this.
查看开源 Forge 项目。 它提供了 JavaScript TLS 实现,以及一些 Flash 来处理实际的跨域请求:
http://github.com/digitalbazaar/forge/blob/master/README
简而言之,Forge 将使您能够从通过 http 加载的网页到 https 站点发出 XmlHttpRequest。 您需要通过服务器提供 Flash 跨域策略文件才能启用跨域请求。 查看自述文件末尾的博客文章,以获得有关其工作原理的更深入的解释。
不过,我应该提到 Forge 更适合两个不同 https 域之间的请求。 原因是存在潜在的 MiTM 攻击。 如果您从非安全站点加载 JavaScript 和 Flash,则可能会受到损害。 最安全的使用是从安全站点加载它,然后使用它访问其他站点(安全或其他)。
Check out the opensource Forge project. It provides a JavaScript TLS implementation, along with some Flash to handle the actual cross-domain requests:
http://github.com/digitalbazaar/forge/blob/master/README
In short, Forge will enable you to make XmlHttpRequests from a web page loaded over http to an https site. You will need to provide a Flash cross-domain policy file via your server to enable the cross-domain requests. Check out the blog posts at the end of the README to get a more in-depth explanation for how it works.
However, I should mention that Forge is better suited for requests between two different https-domains. The reason is that there's a potential MiTM attack. If you load the JavaScript and Flash from a non-secure site it could be compromised. The most secure use is to load it from a secure site and then use it to access other sites (secure or otherwise).
您可以尝试在 iframe 中加载 https 页面,并通过某个桥将所有 ajax 请求路由进/出框架,这是一种 hackaround,但它可能会起作用(不确定它是否会在给定安全上下文的情况下施加相同的访问限制) 。 否则,用于重新路由请求(如任何跨域调用)的本地 http 代理将是可接受的解决方案。
You could attempt to load the the https page in an iframe and route all ajax requests in/out of the frame via some bridge, it's a hackaround but it might work (not sure if it will impose the same access restrictions given the secure context). Otherwise a local http proxy to reroute requests (like any cross domain calls) would be the accepted solution.
这就是我所做的:
使用您想要发布的数据生成一个隐藏的 iFrame。 由于您仍然控制该 iFrame,因此同源不适用。 然后将该 iFrame 中的表单提交到 ssl 页面。 然后,ssl 页面会重定向到带有状态消息的非 ssl 页面。 您可以访问 iFrame。
Here's what I do:
Generate a hidden iFrame with the data you would like to post. Since you still control that iFrame, same origin does not apply. Then submit the form in that iFrame to the ssl page. The ssl page then redirects to a non-ssl page with status messages. You have access to the iFrame.