AJAX TypeError:查询未定义

时间:2017-03-13 09:21:58

标签: c# ajax asp.net-mvc http-post

我正在尝试在C#中对我的控制器执行ajax请求,但它从未到达控制器 - 我得到一个类型错误,说“查询未定义”。

这是我的ajax脚本:

$(document).ready(function () {
    $.ajax({
        url: '/Account/GetAllGamesWithRoles',
        type: 'POST',
        data: {},
        success: function (games) {
            debugger;
            Games = games;

            BuildGames(games);
        },
        error: function() {
        }
    });
});

这是我的控制器动作:

[HttpPost]
public ActionResult GetAllGamesWithRoles()
{
    var result = MockGames();

    return new JsonResult{ Data = result, MaxJsonLength = Int32.MaxValue};
}

1 个答案:

答案 0 :(得分:0)

试试这个

 $(document).ready(function () {
    alert('called before ajax');
    $.ajax({
        url: "/Account/GetAllGamesWithRoles",           
        type: "POST",
        data: {'test':'testcall'},
        success: function (data) {               
            Games = data.Data;
            BuildGames(Games);
        },
        error: function (request, textStatus, errorThrown) {
          alert("Status: " + textStatus + "Error: " + errorThrown);
        }
    });
});


[HttpPost]
public JsonResult GetAllGamesWithRoles(string test)
{
    var result = MockGames();

    return Json{ Data = result, JsonRequestBehavior.AllowGet};
}