简体   繁体   English

FireFox的UTF-16字符编码问题

[英]UTF-16 character encoding issue with FireFox

I'm encountering an issue with UTF-16 encoded XML and Firefox that I can't seem to resolve. 我遇到了UTF-16编码的XML和Firefox的问题,我似乎无法解决。

I'm working with the TFS11 web access API in order to create a custom work item control. 我正在使用TFS11 Web访问API来创建自定义工作项控件。 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. 应该是无关紧要的,但足以说我不能随意乱搞任何Web服务器设置或应用程序发送数据的方式。

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. 一般工作流程是控件下拉附件文件,将其解析为XML,然后使用XML以便为最终用户呈现数据。 This all works perfectly in IE8, IE9, and Chrome. 这一切都在IE8,IE9和Chrome中完美运行。 However, in FireFox 11, it does not. 但是,在FireFox 11中,它没有。

Here's a snippet of code that demonstrates how I'm grabbing my XML and parsing it: 这是一段代码,演示了我如何抓取我的XML并解析它:

$.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 . 问题是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). 在FireFox中,它包含 <? 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 " ? > (依此类推)。

The first two characters are FF FE. 前两个字符是FF FE。 I've read up on it, and I know that this is the Byte Order Marker. 我已经阅读了它,我知道这是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.... 看起来Firefox并没有查看数据中的BOM,因此没有意识到您的数据是UTF-16而不是UTF-8 ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM