【问题标题】:Ajax call in mvc5 always returning errormvc5中的Ajax调用总是返回错误
【发布时间】:2014-05-05 00:33:16
【问题描述】:

我的控制器操作(在 AjaxMethodsController 中)是:

public ActionResult CreateGroup(String groupName)
        {
            ApplicationUser user;
            var userName = User.Identity.Name;
            using (DAL.GDContext context = new DAL.GDContext())
            {
                user = context.Users.FirstOrDefault(u => u.UserName == userName);                              
                if (user != null)
                {
                    var group = new Group();
                    group.GroupName = "test";
                    group.Members.Add(user);

                    context.Groups.Add(group);
                    context.SaveChanges();
                }
            }

            return View();
        }

应该简单地创建具有指定组名的组。我的 ajax 和 jquery 代码是:

$(function () {
    $('#CreateGroup').on("click", function () {
        var groupName = "testgroup";
        $.ajax({
            url: "/AjaxMethods/CreateGroup",
            type: "POST",
            data: JSON.stringify({ 'GroupName': groupName }),
            dataType: "json",
            cache: false,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert("success");
            },
            error: function () {
                alert("An error has occured!!!");
            }
        });
    });
});

它总是返回“发生错误!!!”信息。除了这个问题,如果没有创建组(可能是因为组名已经存在),我如何将其设置为始终作为错误返回。我不希望页面在之后重定向...我应该使用 JsonResult 而不是 ActionResult 吗? 我是 ajax 的新手,大约一个小时后,所以解释而不只是代码会非常有帮助!谢谢!

更新 #1:新的 CreateGroup

public JsonResult CreateGroup(String GroupName)
        {
            ApplicationUser user;
            var userName = User.Identity.Name;
            using (DAL.GDContext context = new DAL.GDContext())
            {
                user = context.Users.FirstOrDefault(u => u.UserName == userName);                              
                if (user != null)
                {
                    var group = new Group();
                    group.GroupName = GroupName;
                    group.Members.Add(user);

                    context.Groups.Add(group);
                    context.SaveChanges();
                }
            }
            string result = userName;
            return Json(result, JsonRequestBehavior.AllowGet);            
        }

【问题讨论】:

    标签: jquery ajax asp.net-mvc


    【解决方案1】:

    您的 ajax 调用将以您使用它的方式接收视图的内容。如果您对将结果呈现为标记不感兴趣,您可能希望将 Json 中的数据返回到您的 ajax 请求。

     string result="OK";
     return Json(result,JsonRequestBehavior.AllowGet); 
    

    【讨论】:

    • 谢谢,我已经尝试了更新 #1 中的内容,但仍然出现错误
    • 你可以使用'@Url.Action("CreateGroups","AjaxMethods")' 设置网址吗?我认为你不需要 JSON.Stringify,因为它将以 json 格式发送默认情况下,您可以只使用 data:{'GroupName':groupName}
    • 试过了,我在 F12 开发工具中得到了这个:POST localhost:49782/AjaxMethods/CreateGroup 500 (Internal Server Error)
    • 查看响应正文。应该有更多的细节。此外,如果您在控制器方法上设置断点,您很可能会追踪到错误。
    • 啊,它没有使用正确的控制器方法!现在好像可以用了,谢谢!有没有办法在控制器上方定义ajax post?好像有[HttpPost],有ajax版本吗?
    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多