#5273 closed bug (invalid)
$.ajax() XML handling problem on IE 7 and IE 8
Reported by: | snoz | Owned by: | |
---|---|---|---|
Priority: | critical | Milestone: | 1.4 |
Component: | ajax | Version: | 1.3.2 |
Keywords: | XML, Internet Explorer, Internet Explorer 7, Internet Explorer 8, IE, IE7, IE8 | Cc: | |
Blocked by: | Blocking: |
Description
Hi,
I browsed around for this and couldn't find this issue..
I have this function:
$.ajax({
url:'http://myurl.com/ajax/?type=getMeXML', cache:true, success:function(xml){
if($("status",xml).text() == "0") return false; p_html = $("info",xml).text(); $("#somediv").html(p_html);
}
});
And this works perfectly on Firefox.
On IE 7 and 8, though, it shows nothing.
I can alert(xml) and it shows the full xml string, but anything other than that is just empty or undefined, like the xml was really just text and jquery couldn't parse it as xml.
The response xml is something like this:
<?xml version="1.0"?>
<response>
<info><p>Homepage</p></info> <status>1</status>
</response>
I even tried with dataType: "text/xml" but the result was exactly the same...
I tried $(xml).find("info").each() etc. and it worked perfectly for Firefox, but IE, nothing...
Let me know if you need something more.
Change History (2)
comment:1 Changed 14 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 Changed 11 years ago by
Because you are in a HTML document. IE won't recognize XML.
console.log($("SELECTION_STATE").get()); returns object HTMLUnknownElement in IE
In order to use the XML you'll have to run it through the IE XML parser. Something like.
var x = new ActiveXObject("Microsoft.XMLDOM"); x.loadXML(yourXML) You'll obviously only want to do this if($.browser.msie)
Side question: Are you loading the XML with AJAX?
Updated: Full Example
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); var myXML = document.body.innerHTML; or wherever you are storing the XML in the DOM xmlDoc.loadXML(myXML)
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError; console.log("You have error " + myErr.reason);
} else {
console.log(xmlDoc.xml);
}
$("SELECTION_STATE", xmlDoc).find("CHARACTERISTIC").each( function() {
if($(this).attr("name") == "Z_MDST" ) {
alert($(this).find("MEMBER").attr("name"));
}
}); from:http://stackoverflow.com/questions/5486366/jquery-xml-parsing-ie7-ie8
If you don't specify a dataType, jQuery tries to figure it out from the headers. I suspect that your server is not returning a content type of xml.
The dataType parameter is "xml", not "text/xml" so that is most likely why it didn't work. In Firefox it will try to parse your xml argument which is most likely a string, and it will succeed. IE will not parse an xml string.
http://docs.jquery.com/Ajax/jQuery.ajax#options