开发者

IE's responseXML.getElementsByTagName() unable to handle non English character?

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), wo开发者_运维问答rks in Firefox and Chrome. If items contain only English characters, it works fine.

Is there a workaround for this ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜