views:

186

answers:

1

Given this XML in data.xml

<?xml version="1.0" encoding="utf-8"?>
<data>
  <bar>100</bar>
</data>

I want to display the content from the "bar" element using the following code in test.html

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
</head>
<body>
<script type="text/javascript">
  $.get('data.xml', function(xml) {
    var foo = $(xml).find('bar').text();
    document.write("<span>foo: [" + foo + "]</span>");
  });
</script>
</body>
</html>

The output in webkit based browsers:

foo: [100]

The output in IE8 on XP:

foo: []

Why do webkit browsers read the element's content correctly while IE8 interprets it as an empty string?

+4  A: 

http://stackoverflow.com/questions/562283/jquery-find-doesnt-return-data-in-ie-but-does-in-firefox-and-chrome

This looks to be very much the same problem. Some highlights:

"If you get messages.xml as the wrong mime type, Internet Explorer won't parse it as XML."

"You need to change the server to send "text/xml" for the content type."

Due credit to Matthew Crumley.

And from jQuery documentation:
"if you specify the $.ajax dataType option to be "xml", make sure your server sends content with the "text/xml" MIME type. Sending the wrong MIME type will prohibit jQuery from correctly managing the data returned in the response"

"use the dataType parameter to load the xml file as text, and parse the returned data within the succes function"

GlenCrawford
+1 Nice one. Found this thread and your answer fixed my issue. Thanks.
Damo