当有新帖子时,很多站点都有通知系统。
例如,
在领英上
单击此按钮后,页面将重新加载。
我想在我的应用程序中实现这种系统。因此,当在数据库中添加新项目时,请显示这种通知,并在用户单击它时显示。重新加载页面。
有没有人知道这种通知的工作方式以及我们如何实施它?关于此的任何帖子或想法都将有所帮助。
另外,如果有人认为此问题可以包含更多标签,请随时添加。
这是一个快速的想法,您可以如何做到这一点。
客户端自动刷新并使用ajax发布获取内容。
<h2>Notification</h2>
<div id="divNotif" style="display:none;"></div>
<script id="sessionJs" type="text/javascript">
function GetNotification() {
$.ajax({
type: "GET",
url: '@Url.Action("GetNotification", "Notification")',
dataType: "html",
success: function (data) {
$("#divNotif").html(data);
$("#divNotif").fadeIn('slow');
setTimeout(function () { GetNotification(); }, 5000); //change timeout duration here
},
error: function (data) {
//alert("BAD:" + data.statusText);
}
});
}
GetNotification();
</script>
控制器返回json
[HttpPost]
public ActionResult GetNotification()
{
int notifCount = fromDb.NotificationCount;
return Content("<span>New notification " + notifCount + "</span>", "text/html"); // returning a PartialView would be better one
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句