我从我的UTF-8数据库中查询名称,如果我用php回显它们,它们在浏览器中打印得很好。
当我用Ajax发回它们时,它们变得无法识别。经过几个小时的调试,我找不到我的错误。
这是ajax代码:
load: function(query, callback) {
if (!query.length) return callback();
$.ajax({
url: 'getnames.php',
type: 'GET',
dataType: 'json',
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
data: {
name: selected_race.concat("__".concat(query)),
},
error: function() {
callback();
},
success: function(res) {
callback(res);
}
});
}
这就是get names.php
<?php
// parameters from URL
$type_entry = $_GET['name'];
$split = explode("__",$type_entry);
$race = "Race_".$split[0];
$name = "%".$split[1]."%";
// connect to the database and get the names
require 'connect_sql.php';
//$names_query = "Select Name FROM " .$race ." WHERE Name Like " .$name;
$names_query = "Select Name FROM ".$race." WHERE Name LIKE '".$name."'";
$query_result = mysqli_query($db, $names_query) or die("Zit hier de fout".mysql_error());;
$names = array();
while($row = mysqli_fetch_array($query_result)){
$names[] = "{ \"Name\": \"$row[0]\" }";
}
//output to browers
header('Content-Type: text/javascript; charset=UTF-8');
echo "[\n" .join(",\n", $names) ."\n]";?>
你看到我做错了吗?