如何控制 AJAX 中的故障函数?

How To Control failure function in AJAX?

我正在尝试处理 AJAX 中的失败函数以向用户显示特定消息。我已经尝试了大部分解决方案,但未能控制故障功能。这是我的代码:

<p id="Likes" class="alert-danger" style="display:none"></p>
<button title="Like" id="Like" data-id="@Model.Book.Book_id" class="like-toggle btn btn-group-sm red fa fa-2x" style="float:initial">❤ @Model.Book.Likes_Count</button>

$('.like-toggle').click(function () {
        var myId = $(this).data('id');

        $.ajax({
            type: "GET",
            url: '@Url.Action("Like", "Book")?Book_id=' + myId,
            success: function (response) {
                document.getElementById("Like").innerHTML = response;
            },
            failure: function (response) {

                $('#Likes').html(response).fadeIn('slow');
                $('#Likes').delay(8000).fadeOut('slow');
            }
        });
    });

此外,这是我的控制器代码:

[HttpGet]
        public ActionResult Like(int? Book_id)
        {
            int state = 0;
            if (User.Identity.IsAuthenticated)
            {
                var x = User.Identity.GetUserId();
                var CheckIfExist = db.Likes.Where(p => p.Book_Id == Book_id && p.User_Id == x.ToString()).FirstOrDefault();
                if (CheckIfExist != null)
                {
                    return Content("You Liked This Book Before !");

                }
                else
                {
                    if (x != null)
                    {
                        var article = db.Books.Find(Book_id);
                        if (article != null)
                        {
                            article.Likes_Count += 1;
                            state = db.SaveChanges();
                            if (state == 1)
                            {
                                Like likes = new Like
                                {
                                    Book_Id = article.Book_id,
                                    User_Id = x
                                };
                                db.Likes.Add(likes);
                                state = db.SaveChanges();
                                if (state == 1)
                                {
                                    return Content(" " + article.Likes_Count.ToString());
                                }
                                else
                                {
                                    return Content("Failed To Save Your Like To DataBase !");

                                }
                            }

                            else
                            {
                                return Content("Failed To Save Your Like To DataBase !");

                            }

                        }
                        else
                        {
                            return Content("Book Does Not Exists In DataBase !");

                        }
                    }
                }


            }


            return Content("Only Registered Members Can Use Our Like System , Please Register To Be Able To Use Our Like System !");
        }

当我测试我的代码时,它运行良好,但 return 值始终指向成功,在我的代码中,成功的唯一值应该是更新其他类似的数字所有的按摩应该显示在:

<p class="Likes"></p>

如何控制我想要return失败或成功的内容?

我通过这样做找到了解决方案:

$('.like-toggle').click(function () {
    var myId = $(this).data('id');

    $.ajax({
        type: "GET",
        url: '@Url.Action("Like", "Book")?Book_id=' + myId,
        success: function(resp) {
            if(resp.success) {
                document.getElementById("Like").innerHTML = resp.responseText;
            } else {
                $('#myElem').html(resp).fadeIn('slow');
                $('#myElem').delay(8000).fadeOut('slow');
            }


        },



    });
});

在 C# 控制器中:

 return Json(new { success = true, responseText = value.ToString() }, JsonRequestBehavior.AllowGet);