jQuery:UTF-8 不适用于 ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14198907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:21:08  来源:igfitidea点击:

jQuery: UTF-8 not working with ajax request

jqueryhtmlajaxutf-8character

提问by Oskar Persson

I have a php file which uses jQuery.ajax()to grab some data from another php file into a div.

我有一个 php 文件,用于jQuery.ajax()从另一个 php 文件中获取一些数据到一个 div 中。

jQuery.ajax({
    type: 'POST',
    encoding:"UTF-8",
    dataType:"html", 
    contentType: "text/plain; charset=UTF-8",
    url: '/path/data.php',
    success: function(msg) {
        jQuery('#dataBox').html(msg);
    }
});

My problem is that if I have some "special" characters in the data I'm getting through ajax such as ??? then I get the "question mark in a black diamond"-mark. If I open the external file in the browser it works. If I put some special characters on the main page, it works.

我的问题是,如果我通过 ajax 获取的数据中有一些“特殊”字符,例如 ??? 然后我得到了“黑色菱形中的问号”标记。如果我在浏览器中打开外部文件,它就可以工作。如果我在主页上放置一些特殊字符,它就可以工作。

Some simplified code:

一些简化的代码:

data.php:

数据.php:

$mysqli = new mysqli("localhost", "username", "pass", "db");
$mysqli->set_charset("utf8");
$mysqli->query("SET GLOBAL time_zone = '+00:00'");
$stmt = $mysqli -> prepare("SELECT GROUP_CONCAT(sometext) AS mytext FROM `mytable`");
$stmt -> execute();
$results = selectResults($stmt);
$stmt -> close();
$mysqli -> close();

selectResults function:

选择结果函数:

function selectResults($stmt)
{
    $parameters = array();
    $results = array();

    $meta = $stmt->result_metadata();

    while ( $field = $meta->fetch_field() ) {   
     $parameters[] = &$row[$field->name]; 
    }

    call_user_func_array(array($stmt, 'bind_result'), $parameters);

    while ( $stmt->fetch() ) {
      $x = array();
      foreach( $row as $key => $val ) {
         $x[$key] = $val;
      }
      $results[] = $x;
    }

    return $results;

}

data.php:

数据.php:

foreach($results as $result){
 $textArray = explode(',', $result['mytext']);
}
foreach($textArray as $text){
echo($text);
}

回答by Esailija

Sorry but if you get the unicode replacement character ?, then the input has been interpreted in UTF-8 but the raw bytes of the input did not match UTF-8, I.E. it was not UTF-8.

抱歉,如果您得到 unicode 替换字符 ?,则输入已被解释为 UTF-8,但输入的原始字节与 UTF-8 不匹配,即它不是 UTF-8。

If you see the correct characters when visiting the page directly, then it must be those ajax parameters forcing UTF-8 interpretation on data that is actually in some other encoding, the default is Windows-1252 for browsers.

如果直接访问页面时看到正确的字符,那么肯定是那些强制对实际采用其他编码的数据进行UTF-8解释的ajax参数,浏览器默认为Windows-1252。

Post the php code and/or the raw bytes of the data.

发布 php 代码和/或数据的原始字节。

回答by Oskar Persson

Fixed it by setting utf8_encode()around the data that I echo()in the data.php file. Didn't think of trying this since it worked fine when opening data.php in the browser.

通过在 data.php 文件中设置utf8_encode()我的数据来修复它echo()。没想到尝试这个,因为它在浏览器中打开 data.php 时工作正常。

EDIT:

编辑:

Moved the utf8_encode()to the selectResults function instead so that it encodes all values that I get from the database.

移动utf8_encode()到selectResults功能代替它编码的所有值,我从数据库中获取。

回答by Alex

I'll write this as an answer since it might help others more easily.

我会写这个作为答案,因为它可能更容易帮助其他人。

To solve the problem you must see to it that the content you are outputting is the same as ajax expects it to be so for utf-8 the content need to be utf-8 or encoded as utf-8 using

要解决该问题,您必须确保您输出的内容与 ajax 期望的内容相同,对于 utf-8 内容需要使用 utf-8 或编码为 utf-8

utf8_encode($content);

If you could provide utf-8 content from the database as Esailija says you would not need to use the encode-function.

如果您可以像 Esailja 所说的那样从数据库中提供 utf-8 内容,您就不需要使用编码功能。