【问题标题】:PHP/AJAX : Can't display foreign character in ajax responsePHP/AJAX:无法在 ajax 响应中显示外来字符
【发布时间】:2017-02-09 15:41:59
【问题描述】:

在我的数据库中,我有外国名字的特殊字符。我在 html 中正常输入该特殊字符时使用了<meta charset="utf-8">,它工作正常。但是,当我调用 ajax 请求并使用 html(data) 将该特殊字符显示到特定 div 中时,文本将更改为 ????? ??????? ????? 如何解决此问题

html

<div id="country_info"> </div>

js

$(document).on('change','#country_list', function(){
     var thisVal_id = $(this).val();
  $.ajax({
    url:'../ajax/paraphernalia/ajax_displayCountry_info.php',
    type:'post',
    data: {thisVal_id : thisVal_id , event_id : event_id},
    cache : false,
    success : function(data){
        $('#country_info').html(data);
    }
 });
});

response.php

//this select option contains different foreign characters
$output .= '<select id="official_name" class="form-control" style="padding:0px; !important">';
        while($row1 = mysql_fetch_assoc($sql1)){
            $output .= '<option value="'.$row1['name_official'].'">'.$row1['name_official'].'</option>';
        }
$output .= '</select>';
echo $output;

【问题讨论】:

  • 你可能想看看这个 stackoverflow answer。希望有帮助!!
  • 谢谢你,但我使用mysql_set_charset("UTF8"); 将我的页面的每个顶部都放在了解决我的问题

标签: javascript php jquery html ajax


【解决方案1】:

只需做一个小改动,这对我有用..

$output .= '<select id="official_name" class="form-control" style="padding:0px; !important">';
    while($row1 = mysql_fetch_assoc($sql1)){
$name=mb_convert_encoding($row1['name_official'], 'HTML-ENTITIES', 'utf-8')
        $output .= '<option value="'.$name.'">'.$name.'</option>';
    }
$output .= '</select>';
echo $output;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2015-06-21
    • 2022-01-09
    • 2021-02-11
    • 2017-10-03
    相关资源
    最近更新 更多