IE 和 Firefox 不显示 UTF-8,而 Chrome 则显示

发布于 2024-12-13 22:06:30 字数 856 浏览 1 评论 0原文

我正在使用 Zend 框架。我尝试通过对操作使用 Ajax 请求来验证表单:

$.ajax({
        type: "POST",
        url: URL_TO_ACTION,
        data: DATA,
        success: function(result,status,xResponse) {
                var error = xResponse.getResponseHeader("error");
                    alert(error);
                },
                error: function(e){
                    alert(e);
                }
      });

在控制器中,我有一个操作来处​​理此问题:

public function validateAction(){
    $response = $this->_response;
    $response->setHeader(
           "error","Hãy chọn một module"
        );      
}

在 IE 和 Firefox 中,它显示“Háy chá»n má»t Module”,而 Chrome 则显示“ Hãy chọn một module”

在布局中我确实有:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

我在这里做错了什么?

I'm using Zend Framework. I try to validate a form by using an Ajax request to an Action:

$.ajax({
        type: "POST",
        url: URL_TO_ACTION,
        data: DATA,
        success: function(result,status,xResponse) {
                var error = xResponse.getResponseHeader("error");
                    alert(error);
                },
                error: function(e){
                    alert(e);
                }
      });

In the controller, I have an Action for handling this:

public function validateAction(){
    $response = $this->_response;
    $response->setHeader(
           "error","Hãy chọn một module"
        );      
}

In IE and Firefox, it says "Hãy chá»n má»t Module" while Chrome says "Hãy chọn một module"

In the layout I do have:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

What did I do wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-12-20 22:06:30

不要依赖 HTTP 标头来传输 ASCII 之外的任何内容。在实体主体中对错误消息进行编码。

长答案:

在我看来,这是 HTTP 标准的一个糟糕的领域。根据HTTP/1.1标准,

HTTP 标头字段,包括通用标头(第 4.5 节)、请求标头(第 5.3 节)、响应标头(第 6.2 节)和实体标头(第 7.1 节)字段,遵循相同的通用格式RFC 822 [9] 第 3.1 节中给出。

RFC 822 说,

3.1.2。标题字段的结构

一旦字段展开,它可以被视为由字段名称后跟冒号(“:”)、字段正文以及回车符/换行符终止组成。 。字段名称必须由可打印的 ASCII 字符组成(即,值介于 33. 和 126. 之间的字符,十进制,冒号除外)。字段主体可以由除 CR 或 LF 之外的任何 ASCII 字符组成。

因此,HTTP 标头是 ASCII。然而,在文档的前面,HTTP/1.1 是这样说的:

TEXT 规则仅用于不打算由消息解析器解释的描述性字段内容和值。仅当根据 RFC 2047 [14] 的规则进行编码时,*TEXT 中的单词才可以包含 ISO-8859-1 [22] 以外的字符集中的字符。

TEXT = <除 CTL 之外的任何 OCTET,
                  但包括LWS>

(4.2 说标题由 TEXT 组成)

任何八位位组序列都与 ASCII 不同,并且文本“仅当根据 ISO-8859-1 [22] 编码时才可以包含来自 ISO-8859-1 [22] 以外的字符集的字符”(对我来说)间接表明标头是 ISO-8859-1。然而,这并不比这句话的全部内容重要:

仅当根据 RFC 2047 [14] 的规则进行编码时,*TEXT 中的单词才可以包含 ISO-8859-1 [22] 以外的字符集中的字符。

事实上,RFC 2047 为我们提供了一种将任何字符集中的任何字符串编码为 ASCII 的方法。 (RFC 2047 是电子邮件如何在主题行或发件人行中包含日语等内容的方式。)

现在是令人悲伤的部分:我认为没有任何主要浏览器实现 RFC 2047。正如您所见,Chrome 对待标头为 UTF-8,Firefox 为 ISO-8859-1。您可以以 RFC 2047 或类似的方式(例如 base64)发送它,并在 javascript 中对其进行解码,但此时,您也可以只在正文中发送它。

Don't rely on HTTP headers being able to transmit anything outside of ASCII. Encode your error message in your entity body.

The long answer:

This is, in my opinion, a sucky area of the HTTP standard. According to the standard for HTTP/1.1,

HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822 [9].

RFC 822 says,

3.1.2. STRUCTURE OF HEADER FIELDS

Once a field has been unfolded, it may be viewed as being composed of a field-name followed by a colon (":"), followed by a field-body, and terminated by a carriage-return/line-feed. The field-name must be composed of printable ASCII characters (i.e., characters that have values between 33. and 126., decimal, except colon). The field-body may be composed of any ASCII characters, except CR or LF.

So, thus, HTTP headers are ASCII. However, earlier in the document, HTTP/1.1 has this to say:

The TEXT rule is only used for descriptive field contents and values that are not intended to be interpreted by the message parser. Words of *TEXT MAY contain characters from character sets other than ISO-8859-1 [22] only when encoded according to the rules of RFC 2047 [14].

TEXT           = <any OCTET except CTLs,
                  but including LWS>

(and 4.2 says headers are composed of TEXT)

any octet sequence is not the same as ASCII, and the text "MAY contain characters from character sets other than ISO-8859-1 [22] only when encoded according" seems (to me) to suggest indirectly that headers are ISO-8859-1. However, that's less important than the whole bit of that sentence:

Words of *TEXT MAY contain characters from character sets other than ISO-8859-1 [22] only when encoded according to the rules of RFC 2047 [14].

Indeed, RFC 2047 gives us a means to encode any string, in any character set, into ASCII. (RFC 2047 is how emails can contain things like Japanese in a Subject line or a From line.)

Now for the sad part: I don't think any major browsers out there implement RFC 2047. And as you've seen, Chrome treats headers as UTF-8, Firefox as ISO-8859-1. You could send it encoded in RFC 2047, or something simliar, like base64, and decode it in the javascript, but at that point, you may as well just send it in the body.

想念有你 2024-12-20 22:06:30

听起来很明显,但是您是否清除了浏览器缓存,或者尝试在 Ajax 请求中设置“cache: false”?

Sounds obvious but have you cleared your browsers cache, or tried setting "cache: false" in your Ajax request?

司马昭之心 2024-12-20 22:06:30

HTTP 标头值编码与页面的编码无关。

特别是,标头值通常限制为 ASCII,除非定义标头的规范表明适用 RFC 2047。通过字节膨胀(对于 ASCII 来说是正确的)将字节转换为相应的 Unicode 代码点将为您提供上面看到的 IE 和 Firefox 结果。

HTTP header value encoding has nothing to do with the encoding of your page.

In particular, header values are typically restricted to ASCII unless the spec defining the header says that RFC 2047 applies. Converting bytes to corresponding Unicode codepoints by byte-inflation (which is correct for ASCII) would give you the IE and Firefox results you see above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文