4

我有像这样的服务器的输出

["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"]

我想使用 js 或 jquery 将它转换为 ["alex", "to its right language"]

我试过

function encode_utf8( s )
{
   return unescape( encodeURIComponent( s ) )
}

但无法正常工作

有什么帮助吗?
提前致谢

4

1 回答 1

1

我不确定通过显示的示例输出“LIKE”是什么意思...

但是如果你得到["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"]并将它分配给一个变量,比如

var foo = ["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"];
// alert(foo[1]) results in "��������" which actually means
// the engine has at least tried to resolve the characters

例如,如果您传入正确的字符代码,例如:

var foo = ["alex", "\u003cp\u003emy UTF paragraph\u003c/p\u003e"];
// alert(foo[1]) results in "<p>my UTF paragraph</p>" which seems correct to me...

在浏览器控制台中尝试上面的示例(在当前的 Chrome 中至少对我有用)

另一方面,如果您"\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"每次都收到,那么我假设与评论者类似,您的回复在您使用 JavaScript 处理之前已经搞砸了

This article shows nicely that using Unicode characters is valid for variable naming so the same should apply to string content.

于 2012-06-21T18:10:19.687 回答