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

相关文章

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

SpringBoot中的404错误:原因、影响及解决策略

《SpringBoot中的404错误:原因、影响及解决策略》本文详细介绍了SpringBoot中404错误的出现原因、影响以及处理策略,404错误常见于URL路径错误、控制器配置问题、静态资源配置错误... 目录Spring Boot中的404错误:原因、影响及处理策略404错误的出现原因1. URL路径错

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

MySQL不使用子查询的原因及优化案例

《MySQL不使用子查询的原因及优化案例》对于mysql,不推荐使用子查询,效率太差,执行子查询时,MYSQL需要创建临时表,查询完毕后再删除这些临时表,所以,子查询的速度会受到一定的影响,本文给大家... 目录不推荐使用子查询和JOIN的原因解决方案优化案例案例1:查询所有有库存的商品信息案例2:使用EX