我已經從PHP腳本通過$.post()
返回了2個日期。然後我試圖將結果分解爲兩個變量(console.log下的註釋部分),而不是顯示在一個變量中。通過jquery post()返回多個值
下面是jQuery代碼:
$.post('api/displayDBSetTimes.php', function(data) {
var obj = JSON.parse(data);
var htmlToInsert = obj.map(function (item) {
console.log(item.DB_ST);
/* var db_st = item.DB_ST;
var db_et = item.DB_ET;
return db_st + " " + db_et;*/
return item;
});
$('#oecd').html(htmlToInsert);
});
的PHP如下。我在用JSON發送消息之前在PHP腳本中格式化日期。上述註釋掉jQuery代碼的工作之前,我格式化PHP日期:
<?php
include_once("../include/sessions.php");
$select = "SELECT start_time AS DB_ST, end_time AS DB_ET FROM countdown WHERE tid = 1;";
$query = mysqli_query($dbc, $select);
if($query){
$out = array();
while ($row = $query->fetch_assoc())
{
$out[] = date('D, m-d-Y H:i:s A', strtotime($row['DB_ST']));
$out[] = date('D, m-d-Y H:i:s A', strtotime($row['DB_ET']));
}
// encode array as json and output it for the ajax script
echo json_encode($out);
}
?>
每次我試着參考PHP變量jQuery的$post()
功能(即item.DB_ST
中的console.log)它返回undefined
,但如果我刪除PHP變量引用,則2個日期將作爲一個大字符串返回。我不想那樣。如果我引用數組的一部分,我不知道它爲什麼會返回undefined。
有人可以幫助我嗎? ajax()
會比這更好嗎?非常感謝幫助。
是的!謝謝! – hnewbie
好聽到!祝你有美好的一天,快樂的編碼! –