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

相关文章

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

IDEA中Maven Dependencies出现红色波浪线的原因及解决方法

《IDEA中MavenDependencies出现红色波浪线的原因及解决方法》在使用IntelliJIDEA开发Java项目时,尤其是基于Maven的项目,您可能会遇到MavenDependenci... 目录一、问题概述二、解决步骤2.1 检查 Maven 配置2.2 更新 Maven 项目2.3 清理本

Java空指针异常NullPointerException的原因与解决方案

《Java空指针异常NullPointerException的原因与解决方案》在Java开发中,NullPointerException(空指针异常)是最常见的运行时异常之一,通常发生在程序尝试访问或... 目录一、空指针异常产生的原因1. 变量未初始化2. 对象引用被显式置为null3. 方法返回null

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File

使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能... 目录@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案1