这篇翻译不完整。请帮忙从英语翻译这篇文章。
XMLHttpRequest 的
overrideMimeType
方法是指定一个MIME类型用于替代服务器指定的类型,使服务端响应信息中传输的数据按照该指定MIME类型处理。例如强制使流方式处理为"text/xml"类型处理时会被使用到,即使服务器在响应头中并没有这样指定。此方法必须在send方法之前调用方为有效。Syntax
XMLHttpRequest.overrideMimeType(mimeType)
Parameters
mimeType
- 一个
DOMString
指定具体的MIME类型去代替有服务器指定的MIME类型. 如果服务器没有指定类型,那么XMLHttpRequest
将会默认为"text/xml"
.
Return value
undefined
.
Example
这个样例指定Content-Type为“text/plain",为接受的数据重写ContentType
Note: 如果服务器没有指定一个Content-Type
头, XMLHttpRequest
默认MIME类型为"text/xml"
. 如果接受的数据不是有效的XML,将会出现格”格式不正确“的错误。你能够通过调用 overrideMimeType()
指定各种类型来避免这种情况。
// Interpret the received data as plain text req = new XMLHttpRequest(); req.overrideMimeType("text/plain"); req.addEventListener("load", callback, false); req.open("get", url); req.send();
Specifications
Specification | Status | Comment |
---|---|---|
XMLHttpRequest overrideMimeType() |
Living Standard | WHATWG living standard |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 1 | Edge Full support 12 | Firefox Full support Yes | IE
Full support
11
| Opera Full support Yes | Safari Full support 1.2 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- See implementation notes.
- See implementation notes.