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

相关文章

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

vscode不能打开终端问题的解决办法

《vscode不能打开终端问题的解决办法》:本文主要介绍vscode不能打开终端问题的解决办法,问题的根源是Windows的安全软件限制了PowerShell的运行,而VSCode默认使用Powe... 遇到vscode不能打开终端问题,一直以为是安全软件限制问题,也没搜到解决方案,因为影响也不大,就没有管

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.