tags:

views:

134

answers:

1

I have a javascript that does this (http is your XMLHttpRequest object)

var r = http.responseXML.getElementsByTagName('item');

The issue is variable r is always an empty list if the response contains non-English character (r.length is 0).

The response header is correctly set Content-Type: text/xml;charset=ISO-8859-1

This is what the response from the webserver looks like

<?xml version='1.0' encoding='UTF-8'?>
<d>
  <r>
    <item value="jmob" label="John Möb"/>
  </r>
</d>

It happens only in IE (both IE6 and IE8), works in Firefox and Chrome. If items contain only English characters, it works fine.

Is there a workaround for this ?

A: 

You said the response header was correctly set, but it's not. You're serving a UTF-8 document, so it should be:

Content-Type: text/xml;charset=UTF-8

There could be an issue with IE taking the content-type header literally. I can't say I've ever run into this problem, but I don't use getElementsByTagName because it doesn't work with namespaces. You could use selectSingleNode() or selectNodes() instead:

// Using recursive descent (//) to find all item nodes
var r = http.responseXML.documentElement.selectNodes("//item");

// Specifying the exact path to the item nodes
var r = http.responseXML.documentElement.selectNodes("r/item");

XPath gives you much more power than getElementsByTagName. You might even find that, using the right path, you can ditch one or two of your if statements. For full XPath syntax, see http://msdn.microsoft.com/en-us/library/ms256471(VS.85).aspx.

Andy E
Alternatively, you *are* serving an ISO-8859-1 document, and the `<?xml` declaration is lying! Normally the `charset` declared in the HTTP header should ‘win’, but you certainly don't want to be serving XML with conflicting charset information. Really you don't want to be serving XML in anything but UTF-8.
bobince
@bobince: sound advice, thanks for the addition.
Andy E