JSON 函数不是通过使用 jquery ajax mvc 调用的



我正在使用模型弹出窗口,我想调用控制器,但 json 函数没有调用。 当我在 jquery 数据中使用断点时,请填写文本框,但函数不调用。 请告诉我我错在哪里。

首先,我声明一个变量,然后存储密码文本框值,然后传递密码参数,然后我单击保存并使用断点消息显示取消定义,然后我删除以前的代码,然后我使用此代码它不调用函数。

爪哇语

<script>
function mSavePassword() {
$.ajax({
url: "@Url.Action("ChangePassword")",
type: "GET",
contentType: "application/json;charset=UTF-8",
data: {
Password: $('#txtcurrentpassword').val(),
NewPassword: $('#txtpassword').val(),
ConfirmPassword: $('#txtConformPassword').val()
},
dataType: "json",
success: function (Record) {
alert("Record  Inserted Successfully");
},
});
}
</script>

杰伦函数

public JsonResult ChangePassword(User U)
{
try
{
con = new SqlConnection(constring);
con.Open();
cmd = new SqlCommand("select User_password from BriskSecurity.dbo.Users where User_Id=" + Session["AgentID"] + "", con);
string mPwd = Convert.ToString(cmd.ExecuteScalar());
if (Cryptographer.Encrypt(U.Password.Trim()) != mPwd.Trim())
{
TempData["old"] = "Incorrect Password";
return Json(TempData["old"], JsonRequestBehavior.AllowGet);
}
if (U.NewPassword.Trim() != U.ConfirmPassword.Trim())
{
TempData["Wrong"] = "Your New Password and Confirm Password do not match";
return Json(TempData["Wrong"], JsonRequestBehavior.AllowGet);
}
U.ConfirmPassword = Cryptographer.Encrypt(U.ConfirmPassword);
cmd = new SqlCommand("update BriskSecurity.dbo.Users set User_password='" + U.ConfirmPassword + "' where User_ID=" + Session["AgentID"] + "", con);
cmd.ExecuteNonQuery();
con.Close();
TempData["PSuccess"] = "Your password has been changed successfully";
}
catch (Exception)
{
TempData["Error"] = "Password not changed due to an error Try Again";
return Json(TempData["Error"], JsonRequestBehavior.AllowGet);
throw;
}
return Json("", JsonRequestBehavior.AllowGet);
}

我会建议从C# MVC页面将动态URL发送到ajax javscript的方法。

我很容易使用这样的表格:

<form method="post" action="@Url.Action("ChangePassword")">
<input ... />
<input ..../>
<input type="submit" value="Change password" />
</form>

使用 jQuery:

$("form").on("submit", function(e) {
mSavePassword($(this));
});

function mSavePassword($form) {
$.ajax({
url: $form.prop("action"),
type: $form.prop("method"),
data: {
Password: $('#txtcurrentpassword').val(),
NewPassword: $('#txtpassword').val(),
ConfirmPassword: $('#txtConformPassword').val()
},
dataType: "json",
success: function (record) {
alert("Record  Inserted Successfully");
}
});
}

并在服务器端进行更改:

[HttpPost]
public JsonResult ChangePassword(User U)

否则,将不起作用

最新更新