我有一个 php 文件,我从数据库中提取数据,我想在 javascript 中访问它的内容。当我尝试使用 data[0].card_id 访问数组时,我得到“未定义”。
这是我的 javascript
$(document).ready(function() {
var userId = 1;
var updateUrl;
$.ajax({
type: "POST",
url: "url",
data: {userId: userId},
success: function(data) {
alert(data[0].card_id);
var suffix = ".html";
fb.start('../Animations/' + updateUrl[0].card_id + suffix);
}
});
}
这是我的 php 文件
<?php
include('connect.php');
$user_id = $_POST['userId'];
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL";
}
$select = "SELECT card_id FROM decks WHERE id=$user_id ORDER BY order_num";
$result = mysqli_query($db, $select);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$animation[] = array(
'card_id' => $row['card_id'],
);
}
json_encode($animation);
echo $animation;
mysqli_close($db);
?>
该数组包含以下数据
数组 ( [0] => 吃 [1] => 木乃伊
ETC.. )