【问题标题】:Youtube API in IE9 and lowerIE9 及更低版本中的 Youtube API
【发布时间】:2015-12-18 07:12:19
【问题描述】:

我正在使用 Youtube API (https://developers.google.com/youtube/v3/?hl=en),特别是:播放列表、播放列表项和要加载的频道。

这适用于所有浏览器,例如 IE9 及更低版本。在这里我总是收到"No Transport" 错误。

这是一个简单的示例代码:

<!Doctype html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Youtube API Test</title>
</head>    
<body>
    <code>    
    </code>    
    <script src="Scripts/jquery-2.1.4.js"></script>  
    <script>
        $.ajax({
            cache: false,
            type: 'GET',
            crossDomain: true,
            url: 'https://www.googleapis.com/youtube/v3/playlistItems',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                $('code').text(JSON.stringify(data, null, 4));
            },
            error: function (err) {
                $('code').text(JSON.stringify(err, null, 4));
            }
        });
    </script>
</body>
</html>

(注意我删除了参数,在 Chrome 和 FF 中这会返回一个 youtube 错误,在 IE 中是一个“No Transport”错误)。

我尝试使用 xdomain 插件 (https://github.com/jpillora/xdomain)

XDomainRequest 插件 (https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest#instructions)

建议here,但没有成功。我缺少什么,这对其他人有用吗?

【问题讨论】:

    标签: jquery youtube-api internet-explorer-8 internet-explorer-9


    【解决方案1】:

    问题是,您不能从 http 站点到 https 服务进行 ajax 调用!我发现的所有示例都失败了:

    1. 请求必须针对与托管页面相同的方案

    此限制意味着如果您的 AJAX 页面位于 http://example.com,那么你的目标 URL 也必须以 HTTP 开头。 同样,如果您的 AJAX 页面位于 https://example.com,那么您的 目标 URL 也必须以 HTTPS 开头。

    我们的意图绝对是阻止 HTTPS 页面 XDomainRequests 用于基于 HTTP 的资源,因为该场景提供了一个 许多开发人员和大多数用户都会遇到的混合内容安全威胁 不明白。

    但是,此限制过于宽泛,因为它阻止了 HTTP 向 HTTPS 页面发出 XDomainRequests 的页面。虽然它是 确实,HTTP 页面本身可能已被泄露,没有 禁止接收公共资源的理由 安全。

    最糟糕的是,Same Scheme 限制意味着 Web 开发人员 使用 file:// 方案在本地测试他们的页面会发现 所有 XDomainRequests 都被阻止,因为 file:// 不匹配 http:// 或 https://,它们是唯一有效的目标方案 (第 1 点)。要解决此问题,Web 开发人员必须托管他们的 本地 Web 服务器上的页面(例如 IIS、Visual Studio 托管 服务器等)。

    来源:http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

    带有 Visual Studio 内置 ASP.NET 开发服务器的 HTTPS:HTTPS with Visual Studio's built-in ASP.NET Development Server

    与 SO 相关:JavaScript cross-domain call: call from HTTP to HTTPS

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 gapi 客户端库,它会自动发现 youtube api 描述并提供 REST 接口的包装器..

      https://developers.google.com/api-client-library/javascript/

      该客户端库声明支持 IE8+,并集成了一些智能代码来绕过浏览器限制..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-19
        • 1970-01-01
        • 2016-06-22
        • 2015-01-15
        • 1970-01-01
        • 2014-09-29
        • 2016-09-10
        • 2012-01-24
        相关资源
        最近更新 更多