0

我确实有一个 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>
4

1 回答 1

0

试试这个,总是使用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>
于 2013-09-13T06:05:56.590 回答