设置多种 MimeTypes
我想知道如果运行此代码会发生什么:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MimeType Tester</title>
<meta charset="utf-8" />
<meta content="text/xml; charset=utf-8" http-equiv="content-type" />
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
</body>
</html>
我的内容将被解析为 XML 或 HTML?两个都?其他?
另外,我有一个 JS 代码设置 mimetype 两次:
req = new XMLHttpRequest();
req.overrideMimeType('text/xml');
req.overrideMimeType('text/html');
我的内容将被解析为 XML 还是 HTML?两个都?其他?
两个代码之间有区别吗? 结果一样吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先考虑 HTML。除非服务器解释
meta http-equiv=content-type
设置并将其转换为真正的HTTP标头(HTML4说这是meta http-的目的) equiv
,但服务器几乎从不这样做),那么 HTTP 内容类型就不会受到影响,而决定是否调用 HTML 或 XML 解析器的正是元 http-equiv 设置(或 doctype)。因此,这通常意味着将调用 HTML 解析器,但问题外部的配置或应用程序代码可能会导致浏览器以不同的方式处理它。 (作为XML,或纯文本,或二进制数据或其他)
至于JS,我不知道也没有测试过它,但想不出为什么第二个 overrideMimeType 调用不会替换设置第一次调用,请求将以
text/html
的内容类型发出。这似乎是此处规范要求的效果:http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-overridemimetype%28%29-method 但它没有明确涵盖这个案例。服务器如何解释从浏览器发送的 mime 类型和内容完全取决于服务器上运行的代码。它可能会考虑 mime 类型来选择解析器,也可能会忽略它,或者做一些完全相反的事情。
Taking the HTML first. Unless the server interprets the
meta http-equiv=content-type
setting and converts it to a real HTTP header (which HTML4 says is the purpose ofmeta http-equiv
, but servers almost never do) then the HTTP content-type is not affected, and it is that, not the meta http-equiv setting (or the doctype) that determines whether the HTML or XML parser is invoked.So that normally means that the HTML parser will be invoked, but configuration or application code external to your question may cause the browser to process it differently. (As XML, or plain text, or binary data or something else)
As for the JS, I don't know and haven't tested it, but can think of no reason why the second overrideMimeType call wouldn't replace the setting of the first call, and the request would go out with a content-type of
text/html
. This seems to be the effect of the requirements of the spec here : http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-overridemimetype%28%29-method but it doesn't explicitly cover this case.How the server interprets the mime type and content sent from the browser is entirely down to the code running on the server. It may take the mime type into account to select a parser, it may ignore it, or do something entirely contrary.