AJAX和YouTube:'X-Frame-Options'到'SAMEORIGIN'。

时间:2022-12-13 18:19:32

I'm writing a site where content is loaded dynamically via jQuery. The reason for this is to leave an audio player running while being able to navigate through page content without refresh.

我正在编写一个网站,通过jQuery动态加载内容。这样做的原因是让音频播放器保持运行,同时能够在不刷新的情况下浏览页面内容。

Ran into trouble trying to get YouTube videos to work, it's setup like this.

试图让YouTube视频正常工作时遇到了麻烦,设置就像这样。

Site content is loaded using .load(), and for the videos page it contains an unordered list of links to YouTube videos, and each with an empty <div> for <iframe>'s. Clicking on a link grows the <li> and embeds the <iframe> with the video.

网站内容使用.load()加载,对于视频页面,它包含一个无序的YouTube视频链接列表,每个视频都有一个空

用于

Everything works well except the <iframe> will not load the source, throwing this error.

一切正常,除了

Refused to display 'http://www.youtube.com/watch?v=myvideo' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

拒绝在一个框架中显示“http://www.youtube.com/watch?v=myvideo”,因为它将“X-Frame-Options”设置为“SAMEORIGIN”。

Also tried adding <iframe> into <li> with display: none; with same result.

还尝试将

I thought I understood the javascript same domain policy, but this is beyond me. Googling the error leads me to believe it's something server side? But that doesn't make any sense.

我以为我理解了javascript相同的域名政策,但这超出了我的范围。谷歌搜索错误让我相信它是服务器端的东西?但这没有任何意义。

Hard to fiddle something like this, hope it makes sense.

很难摆弄这样的东西,希望它有意义。

2 个解决方案

#1


33  

The error means that the document may only be used as iframe inside documents located at www.youtube.com

该错误意味着该文档只能用作位于www.youtube.com的文档中的iframe

Use the URL of the embed-code instead:

请改用嵌入代码的URL:

http://www.youtube.com/embed/myvideo

it will send a X-Frame-Options-header with a value of ALLOWALL

它将发送一个值为ALLOWALL的X-Frame-Options-header

#2


1  

You only need copy the from Youtube Embed section (click on SHARE below the video and then EMBED and copy the entire iframe). You can't add the youtube link directly from url

您只需要复制Youtube Embed部分(点击视频下方的SHARE,然后点击EMBED并复制整个iframe)。您无法直接从网址添加youtube链接

#1


33  

The error means that the document may only be used as iframe inside documents located at www.youtube.com

该错误意味着该文档只能用作位于www.youtube.com的文档中的iframe

Use the URL of the embed-code instead:

请改用嵌入代码的URL:

http://www.youtube.com/embed/myvideo

it will send a X-Frame-Options-header with a value of ALLOWALL

它将发送一个值为ALLOWALL的X-Frame-Options-header

#2


1  

You only need copy the from Youtube Embed section (click on SHARE below the video and then EMBED and copy the entire iframe). You can't add the youtube link directly from url

您只需要复制Youtube Embed部分(点击视频下方的SHARE,然后点击EMBED并复制整个iframe)。您无法直接从网址添加youtube链接