JavaScript,jQuery,AJAX和重音字符:如何使它们工作?

时间:2013-04-12 14:32:56

标签: php jquery ajax utf-8 non-ascii-characters

我遇到了通过AJAX和jQuery显示重音字符的问题。我会更详细地解释一下。

我有一个页面,其中有input字段。当我用4个字符(必须是机场的ICAO代码)填充该字段时,它通过AJAX调用PHP脚本。该脚本如下:

文件: ajax.airport.php

<?php
    include("mysqlexec.inc.php");
    include("functions.inc.php");

    if(isset($_GET['icao'])){ 
        $airportData = GetAirportInfo($_GET['icao']);  

        if (!empty($airportData["Name"])) {
            echo $airportData["Name"];
        } else {
            echo "Not found!";
        }
    }
?>

函数GetAirportInfo();Our Airports网站发出HTTP请求,其中显示机场的名称。如果您可以查看this example page,您可能会注意到ICAO代码是 SBGL ,用户在input上输入并返回GetAirportInfo();函数(它是数组的元素)将是 Galeão - AntônioCarlosJobim Intl ,带有一些重音字符。

问题是,当在文件上回显时,会出现Gale o - Ant nioCarlosJobim Intl

所有文件均为UTF-8(无BOM)。我尝试了几个函数(PHP和JS类型),但事实证明这是不成功的。

在输入上执行的jQuery函数是:

function showAirport(icao, dest) {
    var icao=icao.toUpperCase();

    if (icao.length < 4) {
       $("#"+dest).html("");
    } else {
       $("#"+dest).html('<img src="images/loadingsm.gif"/>');
       $.ajax({
           type    : "GET",
           url     : "ajax.airport.php",
           dataType: "html",
           data    : { icao: icao },
           success : function (result) {
               $("#"+dest).html(result);
           },
       });
    }
};

欢迎任何帮助。

2 个答案:

答案 0 :(得分:0)

检查contentType属性

function showAirport(icao, dest) {
var icao=icao.toUpperCase();

if (icao.length < 4) {
   $("#"+dest).html("");
} else {
   $("#"+dest).html('<img src="images/loadingsm.gif"/>');
   $.ajax({
       type    : "GET",
       contentType: "text/xml;charset=utf-8",
       url     : "ajax.airport.php",
       dataType: "html",
       data    : { icao: icao },
       success : function (result) {
           $("#"+dest).html(result);
       },
   });
}

};

答案 1 :(得分:0)

我认为你应该在ajax.airport.php回显之前编码$airportData["Name"],在ajax的数据回显之前使用json_encode()。其他一些语言需要编码,浏览器可以识别

include("mysqlexec.inc.php");
include("functions.inc.php");

if(isset($_GET['icao'])){ 
    $airportData = GetAirportInfo($_GET['icao']);  

    if (!empty($airportData["Name"])) {
        echo json_encode($airportData["Name"]);
    } else {
        echo "Not found!";
    }
}