如何在Firefox(Jetpack)Addon的response.text中处理编码?

时间:2011-07-06 06:00:46

标签: firefox-addon

我有这样的代码:

Request: require('request').Request
_makeCall:function(callback){
Request({
        url: 'URL-TO-API',
        contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
        content: {
          op: 'OPERATION-TO-CALL,
          password: 'super-sec,
      user: 'me@gmail.com'
        },
        onComplete: function (response) {
            if(response.status == 200){
                callback(response.text);
            }
            else{
                callback('');
            }
    }
});
req.post();
}

API将返回ISO-8859-1中编码的XML结构。 response.text中返回的数据将包含ö,ä和ö等瑞典字符。不幸的是,这样的字符会显示为 。 我在Panel中使用的html页面,显示文本,如下所示:

<!DOCTYPE html>
<html>
<head>
<meta charset="iso-8859-1">

...

我真的被困在这里,有人对处理这个编码问题有任何想法吗?

1 个答案:

答案 0 :(得分:0)

XMLHttpRequestRequest最有可能基于)默认为UTF-8编码。对于不同的编码,服务器的响应需要在Content-Type标头中指定字符集,例如:

Content-Type: text/html; charset=iso-8859-1

这将确保响应正确转换为Unicode。 <meta>代码无效。