如何在JavaScript中设置X-Frame-options?

发布于 2025-02-12 04:53:28 字数 205 浏览 0 评论 0原文

不久前,我尝试在我的网站上嵌入YouTube视频。它给了我一个错误:它的X-Frame-Options设置为Sameorigin,因此无法加载。经过一项研究,我了解到X-Frame-options是一个属性,它控制了是否可以将网页嵌入到其他网页上。如何在网页上的JavaScript代码中设置X-Frame-Options标头?如果不可能,请指导我如何控制服务器端?

A while ago, I tried to embed a youtube video on my website. It gave me an error: that its X-frame-options was set to sameorigin and therefore could not load. After some research I learned that X-frame-options was an attribute which controlled whether a webpage could be embedded onto a different webpage. How can I set the X-frame-options header in my javascript code on my webpage? If this is not possible, please may you guide me to how I could control this on the server side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2025-02-19 04:53:28

与JavaScript? 你不能。

< meta>元素中设置X-Frame-Options是没有用的!例如,< meta http-equiv =“ x-frame-options” content =“ deny”>没有效果。不要使用它! X-Frame-Options仅通过通过HTTP标头设置来工作,

只能从服务器中完成。

从服务器中,它很简单 - 只是不要设置这样的标头。要发送标题,您必须明确执行此操作,因此,如果忽略设置标头的代码,则不会发送标头。 (不过,请注意,有一些服务,例如头盔 这对您而言会改变标题 - 这取决于您的内容使用。

With JavaScript? You can't.

Setting X-Frame-Options inside the <meta> element is useless! For instance, <meta http-equiv="X-Frame-Options" content="deny">has no effect. Do not use it! X-Frame-Options works only by setting through the HTTP header,

It can only be done from the server.

From the server, it's simple - just don't set such a header to begin with. For the header to be sent, you have to do so explicitly, so if you leave out code that sets the header, the header won't be sent. (Though, note that there are some services like Helmet that alter headers for you - it depends what you're using. Figure out what you have, if it's setting headers, and then use whatever the appropriate command is to not send such headers.)

丢了幸福的猪 2025-02-19 04:53:28

x-frame-options是响应标头,即它是从您向您提出请求的服务器发送给您的(在这种情况下为YouTube),这不是您可以通过JavaScript更改的内容。您可以从该YouTube视频页面中获取HTML代码以嵌入YouTube视频。如果您去那里并单击共享按钮,则有一个嵌入式选项,可为您提供所需的代码。使用提供的代码应围绕X-Frame-Options问题。

请参阅 https://support.google.com/youtube.com/youtube/youtube/youtube/answer/answer/171780?hlll171780? = en 有关嵌入Google支持文档视频的更多信息。

参见 https://developer.mozilla.org/en-us/docs/web/http/headers/x-frame-options 有关X-Frame-Options标头的详细信息。

X-Frame-Options is a response header i.e. it is sent back to you from the server you're making the request to (in this case youtube), it's not something you can change via javascript. You can get HTML code for embedding a youtube video from that youtube video's page. If you go there and click on the Share button, there is an Embed option which gives you the code required. Using the code provided should get around the X-Frame-Options issue.

See https://support.google.com/youtube/answer/171780?hl=en for more info about embedding videos from google's support docs.

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options for details about the X-Frame-Options header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文