成功函数未捕获JSON响应

时间:2016-05-21 07:31:28

标签: jquery json ajax asp.net-mvc

我正在对控制器进行简单的ajax调用,该控制器返回一个json对象。 这是代码

<script type="text/x-jquery-tmpl">
$(function () {
    var submitButton = $("#submitButton");
    var infoForm = $("#infoForm");    
    submitButton.click(function() {
        submitInfo(infoForm)
    });
});
function submitInfo(formContainer) {      
    $.ajax({
        type: "POST",
        url: "@Url.Action("ChangePassword", "Account")",
        data: formContainer.serialize(),
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert(data);
        }
    });
}
</script>

控制器:

[HttpPost]
    public JsonResult ChangePassword(ChangePasswordViewModel model)
    {
        //some code
        return Json("ok", JsonRequestBehavior.AllowGet);
    }

这里是Html

<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Change Password</h4>
    </div>
    <div class="modal-body">
        @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary("", new { @class = "text-danger" })

            <fieldset id="infoForm">
                <div class="form-group">
                    @Html.LabelFor(model => model.oldPassword)
                    @Html.PasswordFor(model => model.oldPassword, new { @class = "form-control", required = "" })
                    @Html.ValidationMessageFor(model => model.oldPassword)
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.newPassword)
                    @Html.PasswordFor(model => model.newPassword, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.newPassword)
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.confirmPassword)
                    @Html.PasswordFor(model => model.confirmPassword, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.confirmPassword)
                </div>

                <div class="text-danger">
                    @if (TempData["ErrorMessage"] != null)
                    {
                        @TempData["ErrorMessage"]
                    }
                </div>

                <div class="modal-footer">
                    <button class="btn btn-primary" id="submitButton">Save</button>
                </div>
            </fieldset>
        }
    </div>
</div>
</div>

这是我打电话来显示弹出窗体的部分视图。

但是点击“提交”按钮会返回一个新视图,其上只显示&#34; ok&#34;(带引号)。即使删除alert()后,我也得到了相同的结果。这段代码有问题吗?

2 个答案:

答案 0 :(得分:0)

你的控制器应该是,

[HttpPost]
public JsonResult ChangePassword(ChangePasswordViewModel model)
{
    //some code
    return Json(new { Message="OK"}, JsonRequestBehavior.AllowGet);
}

答案 1 :(得分:0)

尝试将async:false添加到ajax调用或将整个Ajax调用代码块保留在click函数中。