我有一个方法将json数据返回到我的mvc视图,不知道为什么我的视图显示json数据而不是我成功的部分。这是我的Post方法:
[HttpPost]
[Route("resetpassword")]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel resetPasswordViewModel)
{
...
if (ModelState.IsValid)
{
if (resetPasswordViewModel.Password == resetPasswordViewModel.ConfirmPassword)
{
var user = Task.Run(() => Service.GetUserByEmailAsync(email)).Result;
if (user != null)
{
userRequest.Id = user.FirstOrDefault().Id;
userRequest.Password = resetPasswordViewModel.Password;
userRequest.Token = token;
await Service.UpdateUserAsync(userRequest);
}
}
else
{
return Json(new { status = "error", message = "Please enter the same value again" });
}
}
return Json(new { status = "success", message = "" });
}
这是我的观点,即模态:
@model Models.ResetPasswordViewModel
@if (Model != null)
{
<div class="page resetPassword">
@using (Html.BeginForm("resetpassword", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="modal" id="reset-password">
<div class="modal-content">
<span class="close">X</span>
<div><input type="email" name="email" id="email" readonly value=@Model.Email /></div>
<div class="create-user-label">Password</div>
....
这是我的ajax功能:
function resetPassword() {
var postResult = null;
var data = {
Email: document.getElementById('email').value
};
var path = "/resetpassword";
var errorMessage = document.getElementById('Message');
$.ajax({
dataType: "json",
contentType: "text/plain",
url: path,
type: "POST",
cache: false,
data: data,
success: function (result) {
postResult = $.parseJSON(result);
alert(postResult.data);
if (result && result.message) {
$('#reset-password').hide();
$('#reset-thank-you').show();
}
},
error: function () { alert("error"); }
});
}
但不是我的观点,我只在屏幕上看到json数据,如:
{"status":"success","message":""}
答案 0 :(得分:0)
你的数据正好经历,看起来你有一些映射问题,你应该试试这个。
$.ajax({
dataType: "json",
contentType: "text/plain",
url: path,
type: "POST",
cache: false,
data: data,
success: function (result) {
alert(result['status']);
if (result['status']== 'success') {
$('#reset-password').hide();
$('#reset-thank-you').show();
}
},
error: function () { alert("error"); }
});
答案 1 :(得分:0)
您的数据将自动从JSON格式转换为Javascript对象。
据推测,您只想在警报中显示消息:
success: function (result)
{
alert(result.data.message);
},