Youtube API IE9 及更低版本

Youtube API in IE9 and lower

我正在使用 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 中这个 returns 一个 youtube 错误,在 IE 中一个 "No Transport" 错误)。

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

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

按照建议 here,没有成功。我遗漏了什么,这对其他人有用吗?

问题是,您无法从 http 站点向 https 服务发出 ajax 调用! ! Esle 我发现的所有例子都失败了:

  1. Requests must be targeted to the same scheme as the hosting page

This restriction means that if your AJAX page is at http://example.com, then your target URL must also begin with HTTP. Similarly, if your AJAX page is at https://example.com, then your target URL must also begin with HTTPS.

It was definitely our intent to prevent HTTPS pages from making XDomainRequests for HTTP-based resources, as that scenario presents a Mixed Content Security Threat which many developers and most users do not understand.

However, this restriction is overly broad, because it prevents HTTP pages from issuing XDomainRequests targeted to HTTPS pages. While it’s true that the HTTP page itself may have been compromised, there’s no reason that it should be forbidden from receiving public resources securely.

Worst of all, the Same Scheme restriction means that web developers testing their pages locally using the file:// scheme will find that all of the XDomainRequests are blocked because file:// doesn’t match either http:// or https://, which are the only valid target schemes (point #1). To workaround this issue, web developers must host their pages on a local web server (e.g. IIS, the Visual Studio hosting server, etc).

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

带有 Visual Studio 内置 ASP.NET 的 HTTPS 开发服务器:

与 SO 相关:

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

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

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