XMLHttpRequest responseXML在IE下的为null的原因接解决办法

2023-11-21 21:58

本文主要是介绍XMLHttpRequest responseXML在IE下的为null的原因接解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在用soapclient.js处理webservice调用的问题,发现XMLHttpRequest responseXML在FF及chrom下都OK,唯独在IE下测试得不到值,req.responseText在所有浏览器下都ok,后经一通分析及google发现为IE的一个bug,不过有办法解决。

先来看下应用场景:

SOAPClient._onLoadWsdl = function(url, method, parameters, async, callback, req)
{
    var wsdl = req.responseXML;

此时alert的wsdl发现始终为空,但是req.responseText却有值,由于后续会用到xml.getElementsByTagName(),所以这里必需要得到xml格式的wsdl文件。

改为如下:

SOAPClient._onLoadWsdl = function(url, method, parameters, async, callback, req)
{
     var parser = new DOMParser();
     var wsdl = parser.parseFromString(req.responseText, 'text/xml');
    
    //var wsdl = req.responseXML;


具体原因及解决方案见如下(注意支持IE9+,不向下兼容):

XMLHttpRequest responseXML in IE10 Release Preview

  • 24

IE10 in Windows 8 Release Preview updatesthe responseXML from an XMLHttpRequest to return a nativeXML document by default. This change applies to IE10’s Standards and Quirksdocument modes, making them interoperable with other modern browsers and consistentwith a “same markup” approach. Compatibility document modes 5, 7, 8, and 9 are unchanged.

This change may impact sites that were expecting responseXML to containan MSXML document and depended on MSXML-specific functionality such as selectNodes. In these cases, you may request that IE10 return an MSXML by settingthe responseType member of your XMLHttpRequest object to 'msxml-document'. If your code does not depend on MSXML-specific functionality, IE10’s native XML document should work for you.

Native XML support in IE9 brought DOM parity to XML and HTML and enabled XML fragmentsto be inserted and rendered directly within a page (even in HTML). IE9 also simplifiedconverting between XML and DOM with the addition of DOMParser and XMLSerializer. IE10 completes this transition by updatingresponseXML to return a native XML document.

Like IE9, IE10 previews before the Windows 8 ReleasePreview returned an MSXML document for responseXML. As a result, retrievinga native document required the additional step of passing responseTextto DOMParser.

var xhr = newXMLHttpRequest();

//...

var parser = newDOMParser();

var doc = parser.parseFromString(xhr.responseText,'text/xml');

// 'doc' contains a native documentin both IE9 and IE10

In the Windows 8 Release Preview, IE10 eliminates the need for an additional DOMParserstep by returning a native document directly via responseXML. Existingcode using DOMParser will continue to work in IE10.

var xhr = newXMLHttpRequest();

//...

var doc = xhr.responseXML;

// 'doc' contains a native documentin IE10’s Standards and Quirks document modes

// it contains an MSHTML documentin IE9 and in IE10’s compatibility document modes

This simplification also applies to the new response property whenresponseType is set to 'document'.

var xhr = newXMLHttpRequest();

xhr.open(method, url, true);

xhr.responseType = 'document';

//...

var doc = xhr.response;

// 'doc' contains a native documentin IE10’s Standards and Quirks document modes

IE10 additionally includes a mechanism to opt-in to retrieving an MSXML document.This can be useful if you still need some MSXML-specific functionality (such as selectNodes) or simply need some extra time to migrate. To do this, setthe responseType of your XMLHttpRequest object to 'msxml-document'.

var xhr = newXMLHttpRequest();

xhr.open(method, url, true);

try { xhr.responseType = 'msxml-document'; } catch(e){}

//...

var doc = xhr.responseXML;

// 'doc' now contains an MSXML documentin IE10’s Standards and Quirks document modes

In theory the assignment should be ignored by other browsers, but in practice somedo throw an exception. You can defend against this using a try/catchstatement, as in the above example.

—Tony Ross, Program Manager, Internet Explorer


链接:http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx



这篇关于XMLHttpRequest responseXML在IE下的为null的原因接解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/405367

相关文章

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Linux samba共享慢的原因及解决方案

《Linuxsamba共享慢的原因及解决方案》:本文主要介绍Linuxsamba共享慢的原因及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux samba共享慢原因及解决问题表现原因解决办法总结Linandroidux samba共享慢原因及解决

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

MySQL错误代码2058和2059的解决办法

《MySQL错误代码2058和2059的解决办法》:本文主要介绍MySQL错误代码2058和2059的解决办法,2058和2059的错误码核心都是你用的客户端工具和mysql版本的密码插件不匹配,... 目录1. 前置理解2.报错现象3.解决办法(敲重点!!!)1. php前置理解2058和2059的错误

Docker镜像pull失败两种解决办法小结

《Docker镜像pull失败两种解决办法小结》有时候我们在拉取Docker镜像的过程中会遇到一些问题,:本文主要介绍Docker镜像pull失败两种解决办法的相关资料,文中通过代码介绍的非常详细... 目录docker 镜像 pull 失败解决办法1DrQwWCocker 镜像 pull 失败解决方法2总

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

Java报NoClassDefFoundError异常的原因及解决

《Java报NoClassDefFoundError异常的原因及解决》在Java开发过程中,java.lang.NoClassDefFoundError是一个令人头疼的运行时错误,本文将深入探讨这一问... 目录一、问题分析二、报错原因三、解决思路四、常见场景及原因五、深入解决思路六、预http://www

关于Docker Desktop的WSL报错问题解决办法

《关于DockerDesktop的WSL报错问题解决办法》:本文主要介绍关于DockerDesktop的WSL报错问题解决办法的相关资料,排查发现是因清理%temp%文件夹误删关键WSL文件,... 目录发现问题排查过程:解决方法其实很简单:重装之后再看就能够查到了:最后分享几个排查这类问题的小www.cp