简体   繁体   English

HTML片段的内容类型

[英]Content type for HTML fragments

When a server sends an HTTP response with an HTML document in the body, it would usually use the text/html content type. 服务器在正文中发送带有HTML文档的HTTP响应时,通常会使用text/html内容类型。 Should the content type be different if the reponse is a fragment of HTML? 如果响应是HTML的一部分,内容类型是否应该不同?

For example, if the request is AJAX from a client script and the entire response body is <div><p>New text</p></div> then the response is not an HTML document. 例如,如果请求是来自客户端脚本的AJAX,并且整个响应正文是<div><p>New text</p></div>则响应不是HTML文档。 Should the application set the content type to something other than text/html for such fragments? 应用程序是否应将此类片段的内容类型设置为text/html以外的其他类型? If so, what? 如果是这样,该怎么办?

It's a personal preference. 这是个人喜好。 If it is only your app, then it doesn't matter. 如果只是您的应用程序,那就没关系了。 I would keep it text/html because it is still HTML markup, even if not a complete document. 我将其保留为text/html因为它仍然是HTML标记,即使不是完整的文档也是如此。

In regards to XML/HTML document fragments, other than the xml-fragment referenced in the comments (now labeled as "no longer maintained"), there don't appear to be any explicit, official references to document fragments and the content type header. 关于XML / HTML文档片段,除了注释中引用的xml-fragment (现在标记为“不再维护”)之外,似乎没有任何对文档片段和内容类型标头的明确官方引用。 However, some points to consider: 但是,需要考虑以下几点:

  • The xml-fragment spec treats full documents and fragments the same in regards to the Content-Type . xml-fragment规范将完整文档和片段视为与Content-Type

  • The MDN documentation on MIME Types makes no distinction between full documents and fragments ( emphasis added): 关于MIME类型的MDN文档在完整文档和片段之间没有区别( 强调 ):

All HTML content should be served with this type. 所有HTML内容都应使用这种类型。 Alternative MIME types for XHTML (like application/xml+html) are mostly useless nowadays (HTML5 unified these formats). 如今,XHTML的其他MIME类型(例如application / xml + html)几乎没有用(HTML5统一了这些格式)。

  • The W3 spec 8.4 Parsing HTML Fragments explicitly lays out cases for handling an HTML document fragment. W3规范8.4解析HTML片段明确规定了处理HTML文档片段的情况。 Unless the parser fails (hits a parser error), it assumes the string given is HTML. 除非解析器失败(遇到解析器错误),否则它将假定给定的字符串为HTML。 Furthermore, invalid/partial HTML is received by browsers extremely frequently, and is rendered to the fullest extent possible (as opposed to out right failing). 此外,浏览器会非常频繁地接收无效/部分HTML,并将其呈现到最大程度(与完全失败相对)。

    The minimum tags required for a full void HTML document are: 完全无效的HTML文档所需最少标记为:

    • The DOCTYPE: <!DOCTYPE html> - declares the document mode, notably the spec requires them " for legacy reasons " DOCTYPE: <!DOCTYPE html> -声明文档模式,特别是规范要求它们“ 出于传统原因
    • The title: <title>My Page</title> 标题: <title>My Page</title>

    Omission of the these required elements does not change the nature of the content. 省略这些必需元素不会更改内容的性质。 In a practical sense, <p>hello world is still nearly-universally interpreted to as HTML, it is just not a valid document. 从实际意义上讲, <p>hello world仍然几乎被普遍解释为HTML,它只是无效的文档。

  • The RFC standard defining MIME types only explicitly defines text/plain , although the RFC Content-Type header spec references text/html . 尽管RFC Content-Type标头规范引用了text/html ,但是定义MIME类型的RFC标准仅明确定义了text/plain This obviously provides no clear guidance, but also defines no possible alternatives. 显然,这没有提供明确的指导,但也没有定义任何可能的替代方法。

Given the only relevant reference from the W3 states full XML documents and fragments be treated the same (and HTML is subset of XML), the W3 fragment parsing algorithm does not make a distinction (and assumes it's receiving HTML), MDN advises against the usage of any alternative headers, and there's no widely accepted (or really even any notable) alternatives, using text/html for document fragments would be the clear choice. 假设W3的唯一相关参考指出完整的XML文档和片段都被视为相同(HTML是XML的子集),则W3片段解析算法没有区别(并假定它正在接收HTML),MDN建议不要使用在任何其他标头中,也没有广泛接受的(甚至实际上甚至没有任何显着的)替代方法,使用text/html作为文档片段将是显而易见的选择。 I could find no precedent to suggest otherwise, and using some custom MIME type would probably just cause confusion (or worse). 我找不到先例可以暗示否则,使用某些自定义MIME类型可能只会引起混乱(或更糟)。

If you really wanted to make a distinction in your application between full documents and fragments, you could wrap it in JSON, or send an additional custom header from your server (I could find no references to any common practice in regards to this, and may just be confusing to other devs). 如果您确实希望在应用程序中区分完整文档和片段,则可以将其包装在JSON中,或者从服务器发送一个附加的自定义标头(我对此没有任何常见做法的引用,并且可能只是让其他开发人员感到困惑)。

Yes, I am also in a fix for this. 是的,我也正在为此解决问题。 But it is perfectly okay to create your own HTTP headers if you are not fully satisfied with the mapping of an existing one to your use case. 但是,如果您不完全满意将现有的HTTP头映射到用例,则可以创建自己的HTTP头。 In that direction, http://tools.ietf.org/html/rfc6648 "X-" based headers are now deprecated. 在此方向上,现在不建议使用基于http://tools.ietf.org/html/rfc6648基于“ X-”的标题。 Basically, as long as you choose a sufficiently unique and meaningful mime type, you're free to invent your own. 基本上,只要选择足够独特且有意义的哑剧类型,您就可以自由发明自己的哑剧类型。 But as @Wrikken mentioned in his comment, that could be problematic. 但是正如@Wrikken在他的评论中提到的那样,这可能是有问题的。 So to avoid all of it, you could fallback on text/html OR do it via a JSON way , and not <div> way. 因此,要避免所有这些,您可以回退到text / html上,或者通过JSON方式(而不是<div>方式)进行操作。 In an ideal and scalable world, server side should be left free from creating HTMLs/DIVs 在理想的和可扩展的世界中,应该避免服务器端创建HTML / DIV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM