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

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值