我正在构建一个通知系统,我只需要最后一个部分来使其完美运行。
当ajax首次加载时,它使用php变量对服务器端脚本查找新内容,如果它找到新内容,它会使用新的notification_id并更新var以获取下一组结果,如果它没有t在url notification_id周围第二次找到任何内容变为null并将空值插入div。
我如何阻止这种情况发生?
<script type="text/javascript">
var notification_id="<?php echo $notification['notification_id'] ;?>";
function loadIt() {
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,
global: true,
dataType:"json",
cache: false,
success: function(data){
if(notification_id < data.notification_id){
$("#notif_actual_text-"+data.notification_id).prepend('<div class="notif_ui"><div class="notif_text"><div id="notif_actual_text-'+data['notification_id']+'" class="notif_actual_text"><img border=\"1\" src=\"userimages/cropped'+data['notification_triggeredby']+'.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ><br /><a href="'+data['notification_id']+'">'+data['notification_content']+' </a><br />'+data['notification_time']+'<br /></div></div></div></div>');
i = parseInt($("#mes").text()); $("#mes").text((i+data.num));
}
notification_id = data.notification_id;
}
});
}
setInterval(loadIt, 10000);
</script>
答案 0 :(得分:0)
试试这个(如果上面的代码来自viewajax.php):
// Simply use isset() to check if $_GET['notification_id'] is set or not
var notification_id="<?php echo isset($_GET['notification_id']) ? $_GET['notification_id'] : $notification['notification_id']; ?>";
或者如果您要将其发送到另一个文件:
<?php
// Let's just say that this is viewajax.php
$notification_id = $_GET['notification_id'];
echo json_encode(array("notification_id" => $notification_id));
?>