且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用FireFox的UTF-16字符编码问题

更新时间:2023-11-24 21:05:58

在你的回应中发送正确的编码?它看起来像Firefox没有在您的数据中查看BOM,所以没有意识到您的数据是UTF-16而不是UTF-8 ....


I'm encountering an issue with UTF-16 encoded XML and Firefox that I can't seem to resolve.

I'm working with the TFS11 web access API in order to create a custom work item control. That should be irrelevant, but suffice it to say that I'm not at liberty to mess with any of the web server settings or the way in which the application sends the data.

The general workflow is that the control pulls down an attachment file, parses it as XML, and then works with the XML in order to render data for the end-user. This all works perfectly in IE8, IE9, and Chrome. However, in FireFox 11, it does not.

Here's a snippet of code that demonstrates how I'm grabbing my XML and parsing it:

$.ajax({
    url: uri,
    async: true,
    dataType: "text",
    complete: function (xhr, status) {
        if (xhr.responseText != null) {
            data = null; 
            if (window.DOMParser) {
                parser = new DOMParser();
                var responseText = xhr.responseText; 
                data = parser.parseFromString(responseText, "text/xml");
            }
        }
    }
});

The problem is the contents of xhr.responseText. In FireFox, it contains ��<?�x�m�l� �v�e�r�s�i�o�n�=�"�1�.�0�"� �e�n�c�o�d�i�n�g�=�"�u�t�f�-�1�6�"�?�> (and so on).

The first two characters are FF FE. I've read up on it, and I know that this is the Byte Order Marker. What I don't understand is why I'm seeing this instead of correctly encoded text.

Here are my response headers from the request:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/octet-stream
Server: Microsoft-IIS/7.5
X-TFS-ProcessId: 0bc401d6-0b5d-47a4-85b8-114344957d22
X-AspNetMvc-Version: 3.0
Content-Disposition: attachment; filename=d187d991-550a-4f49-b379-3bea7f9518c8.xml
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
X-Content-Type-Options: nosniff
X-FRAME-OPTIONS: SAMEORIGIN
Date: Thu, 15 Mar 2012 17:15:00 GMT
Content-Length: 7520

Is there a workaround for this?

Send the right encoding in your response? It looks like Firefox is not looking at the BOM in your data, so doesn't realize your data is UTF-16 and not UTF-8....