0

我只是在学习 AJAX,并且已经调用了我的控制器,但它不起作用。

我遇到的问题是它出错,但警报在半秒后消失,控制台错误非常荒谬。

任何帮助将不胜感激。

我的 jQuery 看起来像这样:

function AjaxVoteClick(itemId) {
    var Url = '@Url.Action("UpVote", "AJAX")' + '?ideaId=' + itemId;

    $.ajax({
        url: Url,
        type: 'POST',
        timeout: 60000,
        dataType: 'json',
        tryCount: 0,
        retryLimit: 3,
        success: function(data) {
            alert("Success");
        },
        error: function (httpRequest, textStatus, errorThrown) {
            alert(httpRequest + "Status = " + textStatus + ", error = " + errorThrown);
        }
    });
}

当我调试时,它使请求正常,并且具有正确的 id,但随后又出现错误。

控制器正在做它应该做的事情,但无论如何我都会展示它:

    [HttpPost]
    public virtual JsonResult UpVote(int ideaId)
    {
        IdeaGenEntities ctx = new IdeaGenEntities();

        var currentIdea = (from m in ctx.Ideas
                          where m.ID == ideaId
                          select m).Single();

        var model = new VoteModel();
        model.Initialize(currentIdea);

        int currentVotes = model.Votes;
        currentVotes += 1;

        currentIdea.Votes = currentVotes;
        ctx.SaveChanges();

        return Json(model);

    }

看着 Firebug 控制台几乎没有。除非我坚持下去,否则错误会在半秒后出现。然后在标题中你有这个:

接受 application/json, text/javascript, / ; q=0.01 Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Content-Type application/json Cookie __utma=111872281.1986143499.1358265304.1358511066.1358786646.8; __utmz=111872281.1358265304.1.1.utmcsr=(直接)|utmccn=(直接)|utmcmd=(无); ASP.NET_SessionId=natvmbqvjfnm11deiek1hngi; .ASPXAUTH=4C0D282B36C410B3FA8271342C22B0A4435D35CD8F2BFE5B58E3F2A643993963F56D3F410A4E586FC44DC68B5737F9E7EEC18FB32E629014DECAA43B818DC49358E618CA73815EB1BBF977CE04342146B7F5EA30BB7D0D2328FC3B75F1C4CA65 Host localhos Referer localhost User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 X-Requested-With XMLHttpRequest

编辑:添加 Http Fox 错误。

它说 NS_BINDING_ABORTED

并显示:

(请求行)POST /AJAX/UpVote?ideaId=1 HTTP/1.1 主机 localhost 用户代理 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 接受 application/json, text/javascript , / ; q=0.01 Accept-Language en-US,en;q=0.5 Accept-Encoding gzip, deflate X-Requested-With XMLHttpRequest Referer localhost Cookie __utma=111872281.1986143499.1358265304.1358511066.1358786646.8; __utmz=111872281.1358265304.1.1.utmcsr=(直接)|utmccn=(直接)|utmcmd=(无); ASP.NET_SessionId=natvmbqvjfnm11deiek1hngi; .ASPXAUTH=4C0D282B36C410B3FA8271342C22B0A4435D35CD8F2BFE5B58E3F2A643993963F56D3F410A4E586FC44DC68B5737F9E7EEC18FB32E629014DECAA43B818DC49358E618CA73815EB1BBF977CE04342146B7F5EA30BB7D0D2328FC3B75F1C4CA65

在内容中它说:加载内容时出错(NS_ERROR_DOCUMENT_NOT_CACHED)

4

2 回答 2

1
$.ajax({
         type: "POST",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         async: false,
         timeout: 60000,
         tryCount: 0,
         retryLimit: 3,
         success: function(data) {
            alert("Success");
         },
         error: function (httpRequest, textStatus, errorThrown) {
            alert(httpRequest + "Status = " + textStatus + ", error = " + errorThrown);
         }
     });

您必须指定内容类型,如果您正在使用 wcf 服务,您的服务必须是这样的

[OperationContract]
    [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

public virtual JsonResult UpVote(int ideaId)
{
    IdeaGenEntities ctx = new IdeaGenEntities();

    var currentIdea = (from m in ctx.Ideas
                      where m.ID == ideaId
                      select m).Single();

    var model = new VoteModel();
    model.Initialize(currentIdea);

    int currentVotes = model.Votes;
    currentVotes += 1;

    currentIdea.Votes = currentVotes;
    ctx.SaveChanges();

    return Json(model);

}

如果您使用网络服务,您的代码必须包含以下前缀

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public virtual JsonResult UpVote(int ideaId)
    {
        IdeaGenEntities ctx = new IdeaGenEntities();

        var currentIdea = (from m in ctx.Ideas
                          where m.ID == ideaId
                          select m).Single();

        var model = new VoteModel();
        model.Initialize(currentIdea);

        int currentVotes = model.Votes;
        currentVotes += 1;

        currentIdea.Votes = currentVotes;
        ctx.SaveChanges();

        return Json(model);

    }
于 2013-05-29T13:41:53.393 回答
0

我要尝试的第一件事是将您的请求从更改POSTGET和:

return Json(model);

return Json(model, JsonRequestBehavior.AllowGet);

于 2013-05-29T13:16:28.667 回答