In my previous article, Undefined XMLHttpRequest.responseXML.xml, I explained that IE 10 no longer returns an XML Document object for xml responses but instead returns a DOM Document. Today’s post is a continuation demonstrating how to get an XML Document from a web service using jQuery.
The most recommended solution to the problem that I have seen on the web to date has been to add the responseType during the beforeSend method as follows:
$.ajax({ url: "http://site.domain/webservice.asmx/getdata", beforeSend: function (jqXHR) { try { jqXHR.responseType = "msxml-document"; } catch(e) { }; }, success: function(data, textStatus, jqXHR) { var xDoc = (data.ownerDocument) ? data.ownerDocument : data; var value = null; if (xDoc.selectNodes !== undefined) { //IE Browser xPath Model value = xDoc.selectNodes("."); } else if (xDoc.evaluate !== undefined) { //Chrome, Mozilla & Safari Browser xPath Model var list = xDoc.evaluate(".", data, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); if (list.snapshotLength > 0) value = list.snapshotItem(0); } alert(value); } });
Depending on which version of jQuery you are using this may or may not work. Thus one programmer suggested a change to jQuery to add support for the responseType property in jQuery’s custom XHR object.
Personally I don’t like modifying code libraries. When a new version comes out I have to remember to re-add my own personal modifications. I believe it is best to use a library exactly as it is making adjustments to my own code as necessary.
I have one project with a jQuery library where the suggested fix didn’t work. So my solution was to analyze the data returned from the web service converting the DOM Document to an XML Document as needed.
$.ajax({ url: "http://site.domain/webservice.asmx/getdata", beforeSend: function (jqXHR) { try { jqXHR.responseType = "msxml-document"; } catch(e) { }; }, success: function(data, textStatus, jqXHR) { data = GetXmlDocument(data, textStatus, jqXHR); var xDoc = (data.ownerDocument) ? data.ownerDocument : data; var value = null; if (xDoc.selectNodes !== undefined) { //IE Browser xPath Model value = xDoc.selectNodes("."); } else if (xDoc.evaluate !== undefined) { //Chrome, Mozilla & Safari Browser xPath Model var list = xDoc.evaluate(".", data, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); if (list.snapshotLength > 0) value = list.snapshotItem(0); } alert(value); } }); function GetXmlDocument(data, textStatus, jqXHR) { //Only IE doesn't support XPath evaluation if (window.ActiveXObject === undefined) return data; //Only IE 10 and > doesn't support Microsoft.XMLDOM if (data.selectNodes) return data; //Convert responseText to XML Document try { var xmlDOM = new ActiveXObject("Microsoft.XMLDOM"); xmlDOM.async = false; xmlDOM.loadXML(jqXHR.responseText); data = xmlDOM; } catch (e) { } return data; }
This set of calls ensures that the necessary xPath support are available for all versions of jQuery, all versions of IE while continuing to support Chrome, Mozilla and Safari.
Happy coding!
[…] If you are looking for a jQuery solution to this problem refer to the article, jQuery Support for IE 10 XMLHttpRequest. […]
Pingback by Undefined XMLHttpRequest.responseXML.xml | Larry Steinle — October 25, 2013 @ 12:50 pm |