0

这是我的代码:

    [HttpPost]
    public ActionResult VoteChampionStrongAgainst(string championStrong, string againstChampion)
    {
        int champStrongId = int.Parse(championStrong);
        int againstChampId = int.Parse(againstChampion);

        string ip = System.Web.HttpContext.Current.Request.UserHostAddress;

        using (EfCounterPickRepository counterPickRepository = new EfCounterPickRepository())
        {
            var existingCounterPick = counterPickRepository.FindAll()
                                                           .SingleOrDefault(x => x.ChampionStrong == champStrongId && x.AgainstChampion == againstChampId);

            //Does this counterpick combination already exist?
            if (existingCounterPick != null)
            {
                //Has this user already voted?
                var existingVote = counterPickRepository.FindVoteForUser(ip, existingCounterPick.CounterPickVoteId);

                //He hasn't voted, add his vote history.
                if (existingVote == null)
                {
                    StrongCounterHistory history = new StrongCounterHistory();
                    history.IPAddress = ip;
                    history.VoteType = true;
                    history.StrongCounterPickVoteId = existingCounterPick.CounterPickVoteId;

                    counterPickRepository.AddStrongPickHistory(history);
                    counterPickRepository.SaveChanges();

                    //Add a total vote the pick.
                    existingCounterPick.TotalVotes++;
                    counterPickRepository.SaveChanges();
                }
                else
                {
                    //Will use this to display an error message.
                    //How to return an "error" that jquery understands?
                }
            }
            else //This combination doesn't exist. Create it.
            {
                //Create it....
                StrongCounterPickVote newCounterPick = new StrongCounterPickVote();
                newCounterPick.ChampionStrong = champStrongId;
                newCounterPick.AgainstChampion = againstChampId;
                newCounterPick.TotalVotes = 1;

                counterPickRepository.CreateNewCounterPick(newCounterPick);
                counterPickRepository.SaveChanges();

                //Assign a vote history for that user.
                StrongCounterHistory history = new StrongCounterHistory();
                history.IPAddress = ip;
                history.VoteType = true;
                history.StrongCounterPickVoteId = newCounterPick.CounterPickVoteId;

                counterPickRepository.AddStrongPickHistory(history);
                counterPickRepository.SaveChanges();
            }

            return View();
        }
    }

这是我的 jQuery 代码:

$(".pick .data .actions .btn-success").click(function () {
    var champStrongId = $(this).data("champstrongid");
    var againstChampId = $(this).data("againstchampid");

    $.ajax({
        type: 'POST',
        url: "/Counterpicks/VoteChampionStrongAgainst",
        data: { championStrong: champStrongId, againstChampion: againstChampId },
        success: function () {
            alert("Great success!");
        },
        error: function (e) {
            alert("Something bad happened!");
            console.log(e);
        }
    });
});

我需要从我的 ActionMethod 返回什么,以便在success:一切正常或error:出现问题时进入代码执行(例如,他已经对这个特定的计数器选择进行了投票?

4

3 回答 3

0

Servlet 应该回答一个“200 OK”的 HTTP 响应。

不知道你的'View' api,但 HttpServletResponse.setStatus(200) 会在 Java 端做。不要忘记,您可以在浏览器中手动请求 AJAX url 以查看它返回的内容..

于 2012-05-01T03:39:25.157 回答
0

这是我要做的一些事情...

public JsonResult VoteChampionStrongAgainst(string championStrong, string againstChampion)    {
    var success = true;

    // Do all of your data stuff

    return Json(new { success = success, error = 'Some error message'});

}

JsonResult 是返回 Json 的特殊 ActionResult。它会自动为浏览器设置正确的标题。将Json()使用 ASP.NET 的内置序列化程序来序列化匿名对象以返回给客户端。

然后用你的jQuery代码......

$.ajax({
        type: 'POST',
        url: "/Counterpicks/VoteChampionStrongAgainst",
        data: { championStrong: champStrongId, againstChampion: againstChampId },
        success: function (json) {
            if (json.success) {
                alert("Great success!");
            }
            else if(json.error && json.error.length) {
                alert(json.error);
            }
        },
        // This error is only for responses with codes other than a 
        // 200 back from the server.
        error: function (e) {
            alert("Something bad happened!");
            console.log(e);
        }
    });

为了让错误触发,您必须返回一个不同的响应代码Response.StatusCode = (int)HttpStatusCode.BadRequest;

于 2012-05-01T03:53:40.850 回答
0

如果您的服务器上有任何错误,您可以返回 500 内部服务器错误,例如

Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.ContentType = "text/plain";
return Json(new { "internal error message"});
于 2012-05-01T04:01:33.903 回答