I am having some headaches with JSON not returning as JSON. I get no errors, just no data. The browser shows the data in the response.
我有一些令人头疼的问题,JSON没有以JSON身份返回。我没有错误,只是没有数据。浏览器显示响应中的数据。
I know it's not returning as JSON because setting the dataType in the AJAX section causes it to display nothing. If I remove the dataType statement, it displays the data.
我知道它不会以JSON形式返回,因为在AJAX部分设置dataType会导致它不显示任何内容。如果我删除dataType语句,它将显示数据。
I've played around with encoding in the connection string, in the queries, as a header, and in the AJAX section all to no avail. My database is in UTF-8, general_ci.
我在连接字符串,查询,头文件和AJAX部分中使用编码都无济于事。我的数据库是UTF-8,general_ci。
AJAX:
AJAX:
$.ajax({
contentType: 'application/json; charset=UTF-8',
data: {'career' : $career},
dataType: 'json',
url: 'functions.php',
success: function(data) {
$("careerdata").html(data);
},
PHP:
PHP:
if (isset($_GET['career'])) {
require_once 'config.php';
$query = $dbconnect->prepare("select * from jobs where Category = :category");
$query->bindParam(':category', $category);
$category = $_GET['career'];
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
$dbconnect = null;
return;
} else {
echo 'No career data found.';
};
Connection file:
连接文件:
try {
$dbconnect = new PDO("mysql:host=$host;dbname=$database", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
};
If any more info is needed, please let me know.
如果需要更多信息,请告诉我。
Working page is https://www.shardsmith.com/career.php and the actual query is https://www.shardsmith.com/functions.php (but it won't work independently because of the GET variable).
工作页面是https://www.shardsmith.com/career.php,实际查询是https://www.shardsmith.com/functions.php(但由于GET变量,它不能独立工作)。
2 个解决方案
#1
0
It seems that the MySQL (php) client does assume another client encoding than UTF-8. You can request the correct encoding using
似乎MySQL(php)客户端确实假设另一个客户端编码而不是UTF-8。您可以使用请求正确的编码
$dbhconnect->exec("SET NAMES utf8");
directly after you connect to the database.
直接连接到数据库后。
Alternatively, you can use PDO::MYSQL_ATTR_INIT_COMMAND (see http://php.net/manual/de/ref.pdo-mysql.php) in your connection config:
或者,您可以在连接配置中使用PDO :: MYSQL_ATTR_INIT_COMMAND(请参阅http://php.net/manual/de/ref.pdo-mysql.php):
$dbconnect = new PDO("mysql:host=$host;dbname=$database", $user,
$pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
And be careful with unchecked frontend parameters: this is an open door for SQL injections. Either use a prepared statement like this:
并且要小心未经检查的前端参数:这是SQL注入的一扇门。要么使用这样的预备语句:
$query = $dbconnect->prepare("select * from jobs where Category = :cat");
$query->execute(array(':cat' => $_GET['career']));
or escape the input paramerers first:
或首先转义输入参数:
$cat = $dbhconn->quote($_GET['career']);
$res = dbhconnect->query("select * form jobs where Category = {$cat}")->fetchAll();
#2
0
I ended up figuring it out myself. The json itself was fine. At some point I completely forgot about each-looping through the key-value pairs in order to actually display them. For example:
我最终自己搞清楚了。 json本身很好。在某些时候,我完全忘记了通过键值对的每个循环,以便实际显示它们。例如:
$.each(data, function(key,value) {
var $itemid = value.ItemID;
var $db = value.DB;
var $job = value.Job;
var $recipe = value.Recipe;
var $level_range = value.Level_Range;
var $level = value.Level;
var $grade = value.Grade;
var $rarity = value.Rarity;
var $complete = value.Complete;
$("careerdata table").append("<tr class=\"recipe\" id=\""+$recipe+"\">"
+"<td>"+$level+$grade+"</td>"
+"<td class=\"icon\"><a href=\"http://na.finalfantasyxiv.com/lodestone/playguide/db/"+$db+"/"+$itemid+"/\" class=\"eorzeadb_link\"><img style=\"background: url(\'/images/items/"+$job+"/"+$itemid+".png\') 0 1px/42px 42px;\" src=\"/images/items/reflection.png\" /></a></td>"
+"<td class=\""+$rarity+"\">"+$recipe+"</td>"
+"<td><input class=\"complete\" type=\"checkbox\" value=\""+$complete+"\"></td>"
+"</tr>");
});
#1
0
It seems that the MySQL (php) client does assume another client encoding than UTF-8. You can request the correct encoding using
似乎MySQL(php)客户端确实假设另一个客户端编码而不是UTF-8。您可以使用请求正确的编码
$dbhconnect->exec("SET NAMES utf8");
directly after you connect to the database.
直接连接到数据库后。
Alternatively, you can use PDO::MYSQL_ATTR_INIT_COMMAND (see http://php.net/manual/de/ref.pdo-mysql.php) in your connection config:
或者,您可以在连接配置中使用PDO :: MYSQL_ATTR_INIT_COMMAND(请参阅http://php.net/manual/de/ref.pdo-mysql.php):
$dbconnect = new PDO("mysql:host=$host;dbname=$database", $user,
$pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
And be careful with unchecked frontend parameters: this is an open door for SQL injections. Either use a prepared statement like this:
并且要小心未经检查的前端参数:这是SQL注入的一扇门。要么使用这样的预备语句:
$query = $dbconnect->prepare("select * from jobs where Category = :cat");
$query->execute(array(':cat' => $_GET['career']));
or escape the input paramerers first:
或首先转义输入参数:
$cat = $dbhconn->quote($_GET['career']);
$res = dbhconnect->query("select * form jobs where Category = {$cat}")->fetchAll();
#2
0
I ended up figuring it out myself. The json itself was fine. At some point I completely forgot about each-looping through the key-value pairs in order to actually display them. For example:
我最终自己搞清楚了。 json本身很好。在某些时候,我完全忘记了通过键值对的每个循环,以便实际显示它们。例如:
$.each(data, function(key,value) {
var $itemid = value.ItemID;
var $db = value.DB;
var $job = value.Job;
var $recipe = value.Recipe;
var $level_range = value.Level_Range;
var $level = value.Level;
var $grade = value.Grade;
var $rarity = value.Rarity;
var $complete = value.Complete;
$("careerdata table").append("<tr class=\"recipe\" id=\""+$recipe+"\">"
+"<td>"+$level+$grade+"</td>"
+"<td class=\"icon\"><a href=\"http://na.finalfantasyxiv.com/lodestone/playguide/db/"+$db+"/"+$itemid+"/\" class=\"eorzeadb_link\"><img style=\"background: url(\'/images/items/"+$job+"/"+$itemid+".png\') 0 1px/42px 42px;\" src=\"/images/items/reflection.png\" /></a></td>"
+"<td class=\""+$rarity+"\">"+$recipe+"</td>"
+"<td><input class=\"complete\" type=\"checkbox\" value=\""+$complete+"\"></td>"
+"</tr>");
});