views:

49

answers:

3

Hi everyone,

I make an ajax call with jQuery's "$.get" that returns a string to a function. This string contains both xml and html, and I have to extract some part of the html with jQuery's selectors, for example:

$.get(
    url,
    function (xml) {
        $(xml).find('something').whatever();
    }
);

In that case, everything works fine with Firefox and cie, assuming that the xml var is a string (headers text/html sent in php to be sure). But in IE, it can't find the "something" tag.

Any idea? This is very annoying!

+1  A: 

Try to set the dataType to html.

$.get(
    url,
    function (xml) {
        $(xml).find('something').whatever();
    },
    'html'
);
Rocket
Well, this is not working. I believe that the default dataType is html anyway.
Florent Hobein
+1  A: 

Since its XML do try the following
$(xml).contents().find('something').whatever()

this something similar to accessing DOM within an Iframe <iframe>
UPDATE - Answer to Floren's comment
$(document).ready(function(){
var txt = "<hello><world/></hello>";
if (window.DOMParser) {
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
}
alert($(xmlDoc).find('world').length);
});

For parsing XML string from a local string variable you would have create a parser object specially for IE, may not be required for FF or others.
But for $.ajax if dataType is specified as 'xml' then this conversion is done automatically and we dont have to create the XML parser. Hope this helps you scenario.

Ajaxe
Actually, the "xml" variable is a string, not an in XML format, so this isn't working neither.
Florent Hobein
Part of the above code was taken from http://www.w3schools.com/xml/xml_parser.asp
Ajaxe
A: 

I tried to simplify the problem:

var test = "<hello><world /></hello>";
alert($(test).find('world').length);

This is working just fine on every browser (displays 1) but not in Internet Explorer (displays 0, only tried on IE7). Do you have a clue to "fix" this problem, without having to change the format of the variable in XML?

Thx!

Florent Hobein