XMLHttpRequest.overrideMimeType()

这篇翻译不完整。请帮忙从英语翻译这篇文章

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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 1Edge Full support 12Firefox Full support YesIE Full support 11
Full support 11
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

See also

文档标签和贡献者

此页面的贡献者: 651291702, xchenGitHub
最后编辑者: 651291702,