我确实有一个UI,当新消息到达时,我必须在其中显示警报。我正在尝试使用 ajax 来做到这一点。我的意思是我确实有一个名为"我的消息"的链接。每当有新消息到达时,我都会显示新的消息徽章。我的代码如下:
<li> @Html.ActionLink("My Messages", "MyMessage", "ForumPost") </li>
var IsRead = "false";
if ( IsRead == "true" ) {
<span id="txtblnk" class="badge badge-success">New_true</span>
} else {
<span id="txtblnk" class="badge badge-success">New Message</span>
}
.badge-success {
background-color: #F80D0D;
}
.label-success[href], .badge-success[href] {
background-color: #F80D0D;
}
.badge {
display: inline-block;
padding: 2px 4px;
font-size: 11.844px;
font-weight: bold;
line-height: 14px;
color: #1EEE2E;
text-shadow: 0 -1px 0 rgba(0,0,0,0.25);
white-space: nowrap;
vertical-align: baseline;
background-color: #F80D0D;
}
.badge {
padding-right: 9px;
padding-left: 9px;
-webkit-border-radius: 9px;
-moz-border-radius: 9px;
border-radius: 9px;
}
这必须在页面加载时...那么如何在页面加载中使用 ajax...
<script type="text/javascript">
var check;
function checkForMessages() {
$.get("/action/controller",
function ( data ) {
//what should I check here from controller?
if ( ) {
//There are new messages
clearInterval( check );
alert("You have mail!");
}
}
}
check = setInterval( checkForMessages, 60000 );
</script>
试试这个,总是使用Url.Action
而不是硬代码URL
<script type="text/javascript">
var check;
function checkForMessages() {
$.get('@Url.Action("ActionName","ControllerName")',
function ( data ) {
//what should I check here from controller?
if ( ) {
//There are new messages
clearInterval( check );
alert("You have mail!");
}
}
}
check = setInterval( checkForMessages, 60000 );
</script>