我有这个方法可以从我的数据库中检索日期列表。
function getdate($id) {
$select = $this->db->query("select * from dates where user_id= '$id' ");
$row = $select->fetchAll(PDO::FETCH_COLUMN);
return $row;
}
我有一个模型文件“load calendar.php”,它调用 getdate 方法:
$dates = $user->getdate($id);
echo $dates;
我希望能够将数组 $dates 存储在我的 js 文件的数组中:
$(document).ready(function() {
var dbdates = new Array();
$.ajax({
type: 'POST',
url: 'loadcalendar.php',
data: { dates:dates },
success: function(response) {
dbdates = response;
alert(dbdates);
}
});
但是,当我向 dbdates 发出警报时,却没有任何结果。我的“getdate”方法有效。我只需要有关 Ajax 调用的帮助。预先感谢您!
最佳答案
在这里分析这些陈述,
$dates = $user->getdate($id);
echo $dates;
getdate()
方法实际上返回一个数组,而您尝试使用 echo $dates;
执行的操作是,您尝试将数组转换为一个字符串,这是行不通的。
相反,json_encode
数组并echo
它,如下所示:
$dates = $user->getdate($id);
echo json_encode($dates);
此外,在 AJAX 请求中添加 dataType: 'json'
设置,如下所示:
$(document).ready(function() {
var dbdates = new Array();
$.ajax({
type: 'POST',
url: 'loadcalendar.php',
data: { dates:dates },
dataType: 'json',
success: function(response) {
dbdates = response;
console.log(dbdates);
}
});
});
关于javascript - 通过ajax发送fetchAll数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811732/