簡體   English   中英

通過ajax發送fetchAll數組

[英]Send fetchAll array via ajax

我有這種方法可以從數據庫中檢索日期列表。

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);
        }

    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM