我们可以将 http 响应 headers 直接添加到 html 页面吗

Can we add http response headers directly into html pages

我需要将 headers 之类的 X-Frame、Cache-control、Pragma 等直接添加到 html 代码中,可能是使用 [=20= 中的属性] 元素?

它用于通过 href link 直接来自目录的帮助页面。

有什么方法可以将 headers 添加到这些 html 中吗?

简而言之:不,你不能。 HTML 文件是 HTTP 响应的 body; headers 必须来自服务器。您可以嵌入 HTML 文件的任何内容都将成为 body.

的一部分

您可以使用 meta 来复制其中的一些。通常不是理想的解决方案,但请查看 meta 标签的 http-equiv 属性。我相信其中很多已在较新的浏览器中弃用。

示例:

<meta http-equiv="Cache-control" content="no-cache"/>

<meta http-equiv="X-Frame-Options" content="sameorigin"/>

<meta http-equiv="pragma" content="no-cache"/>

如果您的 Web 服务器上启用了 php 执行,您可以添加类似这样的内容:

<?php
http_response_code(your_response_code)
?>
rest-of-your-html-code

这将执行 php 设置响应代码的脚本。