我想用阿賈克斯輪詢顯示用戶更新Facebook的追隨者。阿賈克斯結果顯示undefined
因此,我收集此代碼並應用於我的頁面,其中只追加'undefined'一個接一個。
請問我的代碼在這裏有什麼問題。
在下面,我給我的全輪詢腳本及相關文件
我的表名:updateside
id - work_id - parent_id - from_id - to_id - sub - detail - img - created
..........................................................................
AI - work_id, parent_id etc. all data submit by user post form
我的JavaScript
function waitForMsg(){
$.ajax({
type: "GET",
url: "upsidenew.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
if(data) {
$("#updatetime").append('<div class="upbox1">' + data.detail + '</div>');
}
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
}
$(document).ready(function() {
waitForMsg();
});
upsidenew.php
$parent = //collect from other query
date_default_timezone_set('Asia/Dhaka');
$timestamp = date("M j, y; g:i a", time() - 2592000);
$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
$response = array();
while ($row = mysqli_fetch_array($u)) {
$response['from_id'] = $row['from_id'];
$response['parent_id'] = $row['parent_id'];
$response['to_id'] = $row['to_id'];
$response['sub'] = $row['sub'];
$response['detail'] = $row['detail'];
$response['img'] = $row['img'];
$response['time'] = $row['created'];
?><script><?php echo '(Content-Type: application/json)';?></script><?php
echo json_encode($response);
exit;
}
你爲什麼要在循環中退出?循環將只執行一次。如果你只想返回一行,爲什麼你有一個循環? – Barmar
刪除它但結果相同。 – joy
刪除了什麼?我說在我的答案中刪除的行? – Barmar