【问题标题】:Mootools 1.4.5 Request 'encoding' option not workingMootools 1.4.5 请求“编码”选项不起作用
【发布时间】:2014-07-03 02:59:27
【问题描述】:

我试图在我的 ajax 请求标头上强制使用“ISO-8859-1”字符集,但由于某种原因,我无法将其从“UTF-8”更改。我有以下...

var request = new Request({
    url: url,
    method: 'post',
    data: params,
    encoding: 'ISO-8859-1',
    onSuccess: function(textResponse, xmlResponse) {
            //success func
        },
        onFailure: function(xhr) {
            //fail func
        }
    }).send();

即使在配置该设置后,我的请求标头的内容类型在 Chrome 的开发者工具中仍显示“application/x-www-form-urlencoded; charset=UTF-8”。

有什么想法吗?

【问题讨论】:

    标签: javascript ajax character-encoding mootools


    【解决方案1】:

    确实有效。 1 个小问题:设置字符集的代码路径在另一个变量后面 - 需要设置urlEncoded: true

    https://github.com/mootools/mootools-core/blob/master/Source/Request/Request.js#L175-L178

    还有一个 jsfiddle 来演示这个:http://jsfiddle.net/dimitar/3qazx/

    new Request({
        url: '/echo/html/',
        data: {
            html: '<p>Text echoed back to request</p>',
            delay: 0
        },
        method: 'post',
        urlEncoded: true,
        encoding: 'ISO-8859-1',
        onComplete: function(){
            $('target').set('html', this.response.text);
        }
    }).send();
    

    我认为文档不清楚两者之间的关系 - http://mootools.net/docs/core/Request/Request#Request

    它是这么说的,但是嗯:

    • urlEncoded -(boolean:默认为 true)如果设置为 true,则 content-type 标头设置为 www-form-urlencoded + encoding
    • encoding - (字符串:默认为utf-8)要在请求标头中设置的编码。

    后者需要'当 urlEncoded 不正确时忽略'

    【讨论】:

    • 嗯...使用您提供的 JSFiddle 示例,我仍在 Chrome 的开发工具中显示“application/x-www-form-urlencoded; charset=UTF-8”。另外,urlEncoded 不是默认设置为true 吗?我认为您的配置中还有一个错字——您将选项写为urlEncoding 而不是urlEncoded。但是...即使我更改为urlEncoded,它仍然无法正常工作。
    • w3.org/TR/XMLHttpRequest/#the-send%28%29-method - 它被浏览器拒绝。我可以发誓它正在工作 - 甚至在我的剪贴板中有一个截图,然后选择不打扰。 bugzilla.mozilla.org/show_bug.cgi?id=455850 - 话虽如此,如果您查看 jsfiddle.net/yzU26,它在 FF 和 Chrome 中都是 100% 正确的,没有 mootools。非常非常奇怪。你介意在 mootools 问题跟踪器上提出这个问题,以便他们可以在 1.5 版本之前查看它。谢谢!
    • Let the request entity body be the result of running the multipart/form-data encoding algorithm with data as form data set and with utf-8 as the explicit character encoding.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多