【问题标题】:Can we add http response headers directly into html pages我们可以将http响应头直接添加到html页面中吗
【发布时间】:2016-03-22 16:02:27
【问题描述】:

我需要将 X-Frame、Cache-control、Pragma 等响应标头直接添加到 html 代码中,可能是在 html 元素中使用属性?

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

有没有办法给这些 html 添加标题?

【问题讨论】:

标签: html jsp http response-headers


【解决方案1】:

简而言之:不,你不能。 HTML 文件是 HTTP 响应的正文;标头必须来自服务器。您可以嵌入到 HTML 文件中的任何内容都将成为正文的一部分。

【讨论】:

    【解决方案2】:

    您可以使用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"/>
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • OP 想要添加“标题”,而不是使用 HTML 的响应代码
      猜你喜欢
      • 2023-01-01
      • 2018-04-06
      • 2016-03-22
      • 2015-02-13
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多