javascript - Mootools 1.4.5 请求 'encoding' 选项不起作用

标签 javascript ajax character-encoding mootools

我试图在我的 ajax 请求 header 上强制使用“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();

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

有什么想法吗?

最佳答案

它确实有效。 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: defaults to true) If set to true, the content-type header is set to www-form-urlencoded + encoding
  • encoding - (string: defaults to utf-8) The encoding to be set in the request header.

后者需要“当 urlEncoded 不正确时被忽略”

关于javascript - Mootools 1.4.5 请求 'encoding' 选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665090/

相关文章:

javascript - 如何将多行字符串打印到 blob 中?

javascript - 找到正确的纬度和地段

javascript - React 性能测量代码存在内部错误

javascript - 使用 jQuery 解析外部 JSON 文件

javascript - jQuery.html() 在 IE 中出现乱码

encoding - 如何在经典 ASP 中使用 XMLHttp 对象为 POST 设置字符集?

javascript - 如何从 babel 处理的 rollup 包中删除 use strict

javascript - Ajax不发送数据到php文件

javascript - 某些带括号的字符串会导致 Ajax POST 操作失败并出现 403 错误(禁止)

在 C 中从二进制转换为 char