Jquery 成功函数未捕获JSON响应
我对控制器进行这个简单的ajax调用,它返回一个json对象。 这是密码Jquery 成功函数未捕获JSON响应,jquery,json,ajax,asp.net-mvc,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) }); }
<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>
这是Html
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</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>
&时代;
修改密码
@使用(Html.BeginForm(“ChangePassword”、“Account”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(“,new{@class=“text danger”})
@LabelFor(model=>model.oldPassword)
@Html.PasswordFor(model=>model.oldPassword,new{@class=“form control”,required=”“})
@Html.ValidationMessageFor(model=>model.oldPassword)
@LabelFor(model=>model.newPassword)
@Html.PasswordFor(model=>model.newPassword,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.newPassword)
@LabelFor(model=>model.confirmPassword)
@Html.PasswordFor(model=>model.confirmPassword,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.confirmPassword)
@if(TempData[“ErrorMessage”]!=null)
{
@TempData[“ErrorMessage”]
}
拯救
}
这是一个局部视图,我打电话是为了显示一个弹出表单
但是点击submit按钮返回一个新的视图,上面只写着“ok”(带引号)。即使在删除alert()之后,也会得到相同的结果。这个代码有什么问题吗 您的控制器应该是
[HttpPost]
public JsonResult ChangePassword(ChangePasswordViewModel model)
{
//some code
return Json(new { Message="OK"}, JsonRequestBehavior.AllowGet);
}
尝试将async:false添加到ajax调用中,或将整个ajax调用代码块保留在click函数中。您的json响应不包含属性名
消息。只需使用警报(数据)
(或者您可以将控制器代码更改为返回Json(new{Message=“ok”},JsonRequestBehavior.AllowGet);
什么是console.log(数据)代码>您无论如何都没有使用表单提交操作。然后将按钮类型更改为type=“button”
如果带有id=“submitButton”
的元素是提交按钮或输入,则必须取消默认操作(添加返回false;
作为脚本的最后一行-在submitInfo(infoForm)
之后)@StephenMuecke我写错了它。Edited.ok@Resham只需将调试器放入成功函数中,然后查看数据中包含哪些数据?是否有任何数据。消息可用或不可用。。什么不可用,数据或数据。消息?@Bharat,你需要阅读问题中的评论-OP刚刚输入了一个错误,这与问题无关:)
[HttpPost]
public JsonResult ChangePassword(ChangePasswordViewModel model)
{
//some code
return Json(new { Message="OK"}, JsonRequestBehavior.AllowGet);
}