远端数据返回是gb2312而本地js是utf8写到页面是乱码,如何用js脚本把gb2312编码转换成utf8 ?
|
otakustayJun 15, 2015
1
远端返回的时候在Content-Type头里加上charset应该就行了的
|
|
imn1Jun 15, 2015
4
如果自己有后台环境,可以用后台做个跳板,js连接后台某个程序,这个程序把远端接收转码
|
|
oott123Jun 15, 2015
5
https://www.npmjs.com/package/iconv-lite
Convert character encodings in pure javascript. In-browser usage via Browserify (~180k gzip compressed with Buffer shim included). |
|
tuxJun 15, 2015
6
document.charset='gb2312';
|
|
7
var rstr2utf8 = function (input) {
var output = ""; for (var n = 0; n < input.length; n++) { var c = input.charCodeAt(n); if (c < 128) { output += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { output += String.fromCharCode((c >> 6) | 192); output += String.fromCharCode((c & 63) | 128); } else { output += String.fromCharCode((c >> 12) | 224); output += String.fromCharCode(((c >> 6) & 63) | 128); output += String.fromCharCode((c & 63) | 128); } } return output; } |