IE10中的XMLHttpRequest responseXML

来源:互联网 发布:航天信息防伪开票软件 编辑:程序博客网 时间:2024/05/16 09:16

Windows 8 Release Preview 中的 IE10 更新 XMLHttpRequest 的 responseXML,XMLHttpRequest 以便默认返回本机 XML 文档。这一更改适用于 IE10 的标准和 Quirks 文档模式,从而使其能够与其他当代浏览器互操作,并与“相同标记”方法保持一致。兼容性文档模式 5、7、8 和 9 保持不变。

该更改可能影响期望responseXML responseXML 包含 MSXML 文档且依赖于 MSXML 特定功能(如 selectNodes)的站点。selectNodes.在这些情况下,您可通过将 XMLHttpRequest 对象的 responseType 成员设置为“msxml-document”,responseType 来请求 IE10 返回XMLHttpRequest MSXML 文档。'msxml-document'.如果您的代码不依赖于 MSXML 特定功能,则 IE10 的本机 XML 文档可以有效地供您使用。

IE9 中的本机 XML 支持带来了类似于 XML 和 HTML 的 DOM,并支持直接在页面中(甚至在 HTML 中)插入和呈现 XML 片段。通过新增了 DOMParser 和 XMLSerializer,IE9 还简化了 XML 与 DOM 之间的转换。IE10 通过更新 responseXML 以返回本机 XML 文档responseXML 来完成该转换。

与 IE9 类似,IE10 会在 Windows 8 Release Preview 针对 responseXML 返回 MSXML 文档前进行预览。responseXML.结果,检索本机文档需要一个附加步骤,即将 responseText 传递给responseText DOMParser。DOMParser.

var xhr = new XMLHttpRequest(); //... var parser = new DOMParser(); var doc = parser.parseFromString(xhr.responseText, 'text/xml'); // 'doc' contains a native document in both IE9 and IE10

在 Windows 8 Release Preview 中,由于直接通过 responseXML 返回本机文档,DOMParser 因此 IE10 无需这个附加步骤。responseXML.使用 DOMParser 的现有代码DOMParser 可以继续在 IE10 中照常工作。

var xhr = new XMLHttpRequest(); //... var doc = xhr.responseXML; // 'doc' contains a native document in IE10’s Standards and Quirks document modes // it contains an MSHTML document in IE9 and in IE10’s compatibility document modes

当 responseType 设置为“document”时,response 这种简便性也适用于responseType 新 response 属性。'document'.

var xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.responseType = 'document'; //... var doc = xhr.response; // 'doc' contains a native document in IE10’s Standards and Quirks document modes

IE10 还另外包含一种用来检索 MSXML 文档的机制。如果您仍然需要一些 MSXML 特定功能(如 selectNodes),或者只是需要一些额外的时间来进行迁移, selectNodes那么该机制是比较有用的。为此,请将您 XMLHttpRequest 对象的responseType responseType 设置为XMLHttpRequest “msxml-document”。'msxml-document'.

var xhr = new XMLHttpRequest(); xhr.open(method, url, true); try { xhr.responseType = 'msxml-document'; } catch(e){} //... var doc = xhr.responseXML; // 'doc' now contains an MSXML document in IE10’s Standards and Quirks document modes

理论上讲,该分配应被其他浏览器忽略,但实际上,一些浏览器会引发异常。您可以利用 try/catch 语句防止这一弊端,try/catch 如上面的示例所示。

—Internet Explorer 项目经理 Tony Ross

文章引用地址:http://www.iefans.net/ie10-xmlhttprequest-responsexml/ 作者:iefans

原创粉丝点击