您需要使用两次调用-ajax中的1 get和1 post,还是可以将成功/失败的数据发送回去? [英] Do you need to use two calls - 1 get and 1 post in ajax or can you send data back with the success / failure?

查看:79
本文介绍了您需要使用两次调用-ajax中的1 get和1 post,还是可以将成功/失败的数据发送回去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器方法:

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

with the following ajax call:

$(function () {
        $('#CreateGroup').on("click", function () {

            var groupName = $('#groupname').val();
            if (groupName != '') {

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

CreateGroup函数失败,提示未捕获的ReferenceError:数据未定义"

The CreateGroup function fails saying "Uncaught ReferenceError: data is not defined"

我是否必须使用另一个Json请求-输入post-以获得用户名?

Do i have to use another Json request - type post - to get the username?

推荐答案

您可以在不使用JSON.stringify的情况下进行调用.同样,您的控制器方法具有一个高速缓存属性,可以产生更多的控制权.就个人而言,我将使用控制器缓存控件.返回数据之前,您可能会获得控制器调用的缓存版本.

you can do the call without using JSON.stringify. Also your controller method has a cache attribute that may yield more control. Personally, I would use the controller cache control. You are probably getting a cached version of the controller call prior to returning data.

 [OutputCache(NoStore = true, Duration = 0)]
 public ActionResult CreateGroup(string GroupName)



$.ajax({
    url: '@Url.Action("CreateGroup","AjaxMethods")',
    type: "POST",
    data: { 'GroupName': groupName },
    dataType: "json",
    traditional: true,    
    success: function (data, status, xhr ) {
        alert("success");
        CreateGroup(data);
    },
    error: function () {
        alert("An error has occured!!!");
    }
});

注意:更新成功回调.

这篇关于您需要使用两次调用-ajax中的1 get和1 post,还是可以将成功/失败的数据发送回去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆