1

我有一个 ajax 调用来刷新在线朋友,当我启动应用程序时它可以正常工作。但是如果我登录,它有时会停止工作(大多数,但不是所有时间)。

使用 asp-net-mvc5:

    $(document).ready(function () {
        //change the scroll bar
       jQuery('#chatlist').slimScroll({
          color: '#00f',
          size: '10px',
          width: '50px'
       });
       //set periodic refresh of online friends
       var refreshId3 = setInterval("updateOnlineFriendsArea2()", 4000);
    });

   function updateOnlineFriendsArea2() {
      $.ajax({
           url: "/Home/GetOnlineFriedns/",
           dataType: 'json',
           async: true,
           success: function (data) {
                $("#message").html += "Fetching online friends...";
                $("#friendlist").html("");
                var x;
                if (data.length > 0) {
                    for (x in data) {
                      $("#friendlist").html(
                      $("#friendlist").html() +
                      "<li>" +
                      data[x].Username + "</li>");
                    }
               }
               else {
                    $("#chatarea").html("<p>No Messages</p>");
               }
           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert(errorThrown);
           }
       });

       $("#message").html("Messages loaded.");
    }

引发错误的登录控制器方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            // Validate the user password
            if (await Secrets.Validate(model.UserName, model.Password))
            {
                string userId = await Logins.GetUserId(IdentityConfig.LocalLoginProvider, model.UserName);
                await SignIn(userId, model.RememberMe);     
                //test
                LoginHelpers.LogUser(User.Identity.GetUserName());
                return RedirectToLocal(returnUrl);
            }
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError(String.Empty, "The user name or password provided is incorrect.");
        return View(model);
    }

控制器上的在线好友方法:

    [HandleError]
    [AllowAnonymous]
    public JsonResult GetOnlineFriedns()
    {
        UserProfile user = Profiles.GetProfileByUsername(this.HttpContext.User.Identity.Name);
        var result = new LinkedList<object>();
        if (user != null)
        {
            //Microsoft.AspNet.Identity.IUserLogin teste;
            var onlineFriends = ChatMessages.GetOnlineFriedns(user.ID);
            if (onlineFriends != null)
                foreach (var friend in onlineFriends)
                {
                    result.AddLast(new { Username = friend.Username });
                }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }

显示的错误:

语法错误:输入意外结束

我完全迷失了,无法弄清楚可能是什么问题:

  • 登录时的任务?
  • 重定向和部分?
  • ajax配置不好?
4

0 回答 0