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);
        }
    });
});

問題が発生した場合success:、または問題が発生error:した場合 (たとえば、彼はすでにこの特定のカウンター ピックに投票しましたか?

4

3 に答える 3

0

Servlet should answer a "200 OK" HTTP response.

Don't know about your 'View' api, but HttpServletResponse.setStatus(200) would do on the Java side. Don't forget, you can request the AJAX url manually in your browser to see what it is returning..

于 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 に答える