How do I handle encoding in a Firefox (Jetpack) Addon's response.text?
I have code like this:
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'
},
onCompl开发者_JS百科ete: function (response) {
if(response.status == 200){
callback(response.text);
}
else{
callback('');
}
}
});
req.post();
}
The API will return a XML struct encoded in ISO-8859-1. The returned data, in response.text, will contain Swedish characters like ö, ä and ö. Unfortunately characters like this will be displayed as �. The html page I use in the Panel, where the text is displayed, look like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="iso-8859-1">
….
I'm really stuck here, anyone have any idea on handling this encoding issue?
XMLHttpRequest
(which Request
is most likely based on) defaults to UTF-8 encoding. For a different encoding the response of your server needs to specify the charset in the Content-Type
header, e.g.:
Content-Type: text/html; charset=iso-8859-1
This will make sure the response is converted to Unicode correctly. <meta>
tags won't help.
精彩评论