【问题标题】:Javascript automatically converts UTF to ISO-8859Javascript 自动将 UTF 转换为 ISO-8859
【发布时间】:2014-04-02 13:15:17
【问题描述】:

我在编码方面遇到了非常奇怪的问题,这让我发疯。

让我们假设:

function Test(Name, callback) {
    $.ajax({
        type: "POST",
        url: "Test.php",
        data: "Name=" + Name,
        success: function (msg) {
            callback(msg);
        }
    });
}

和 test.php 文件:

function is_utf8($string) {
    return (mb_detect_encoding($string, 'UTF-8', true) == 'UTF-8');
}

    if (is_utf8($_POST["Name"])){
        echo "Im in UTF";
    }
    else{
        echo "im not in UTF!!";
    }

编辑 - 阅读本文 我在 [http://www.w3schools.com/tags/ref_urlencode.asp][http://www.w3schools.com/tags/ref_urlencode.asp] 找到了一些东西 -> 我发现问题是导致编码例如“%21”变成感叹号。如何阻止这种情况发生?我想简单地传递 %21 并在 PHP 中获取字符串 "%21% 字符串,因此当我将其转换为 UTF 时,我得到感叹号。

【问题讨论】:

  • 我不想知道它是什么字符集......这个问题是关于它为什么要转换它。我确切地知道输入字符集是什么。
  • jQuery 会将 data 转换为查询字符串,适合默认的内容类型“application/x-www-form-urlencoded” - 请参阅 processData.ajax() 上。根据同一页面,它仍然应该是 UTF-8。也许尝试设置processData: false。该页面上的示例也使用data: { name: "John", location: "Boston" }
  • 您好,感谢您的回答。我发现问题是导致 Url 编码的字符串,例如“%21%”。我相应地编辑了帖子,你可以看看吗?
  • 你可能只需要使用urldecode

标签: javascript encoding utf


【解决方案1】:

解决方案是使用

encodeURIComponent(Name)

【讨论】:

  • 您能否详细说明您的答案并将其置于问题的上下文中?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 2011-05-26
  • 2014-08-29
  • 2014-04-30
相关资源
最近更新 更多