简体   繁体   English

我的jquery ajax表单发布有什么问题?

[英]what is wrong with my jquery ajax form post?

    $("button[name='advocate-create-button']").on("click", function () {
    $.ajax({            
        url: '/Vendor/AdvocateCreate',
        type: 'post',
        dataType: 'json',
        data: $('form#advocate-create-form').serialize(),
        success: function() {
            $(".modal_overlay").css("opacity", "0");
            $(".modal_container").css("display", "none");
        }    
    }).done(function (data) {
        var $target = $("#advocate-list-view");
        var $newHtml = $(data);
        $target.replaceWith(data);
        $newHtml.effect("highlight");
    });
    return false;
});

Almost got this, just need a slight assist to get it done...I'm trying to post form data to '/Vendor/AdvocateCreate' and once it saves, I want the dialogue to go away and the list behind it to be updated. 几乎可以做到这一点,只需要一点帮助即可完成...我正在尝试将表单数据发布到“ / Vendor / AdvocateCreate”,一旦保存,我希望对话消失并且其背后的列表更新。

The list behind it is the AdvocateList view and pulls its data from AdvocateList method in the same controller 它后面的列表是AdvocateList视图,并从同一控制器中的AdvocateList方法中提取数据

AdvocateCreate method AdvocateCreate方法

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AdvocateCreate(Advocate advocate, int page = 1)
    {
        if (!ModelState.IsValid) return View(advocate);
        db.Advocates.Add(advocate);
        db.SaveChanges();

        var userId = WebSecurity.GetUserId(User.Identity.Name);

        var model = (from a in db.Advocates.ToList()
                     where a.VendorId == userId
                     select a).ToPagedList(page, 15);
        if (Request.IsAjaxRequest()) return PartialView("_AdvocateListPartial", model);
        return View(model);
    }

the form tag is thus: <form class="form" id="advocate-create-form"> 因此,表单标签为: <form class="form" id="advocate-create-form">

The create method does get called, the data does get saved, but the lines under success: do not fire and the data in #advocate-list-view is not updated 确实调用了create方法,确实保存了数据,但成功的行如下:不要触发并且#advocate-list-view中的数据不会更新

thanks 谢谢

Your service looks like it should be returning html, and you're treating it as-if it should be html, so i'm going to assume it's html. 您的服务看起来应该返回html,并且将其视为html,因此,我假设它是html。 You need to either remove the dataType option, or set it to html. 您需要删除dataType选项,或将其设置为html。 Since html is not valid json, jQuery is triggering the error handler rather than success. 由于html不是有效的json,因此jQuery会触发错误处理程序而不是成功。

$("button[name='advocate-create-button']").on("click", function () {
    $.ajax({            
        url: '/Vendor/AdvocateCreate',
        type: 'post',
        /*dataType: 'json',*/
        data: $('#advocate-create-form').serialize(),
        success: function() {
            $(".modal_overlay").css("opacity", "0");
            $(".modal_container").css("display", "none");
        }    
    }).done(function (data) {
        var $target = $("#advocate-list-view");
        var $newHtml = $(data); // dunno what is 
        $target.replaceWith(data); // goin on here
        $newHtml.effect("highlight"); // or here
    });
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM