【问题标题】:json function not call via using jquery ajax mvcjson函数不通过使用jquery ajax mvc调用
【发布时间】:2019-03-13 02:19:43
【问题描述】:

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

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

Javascript

<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>

JSON 函数

 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);

        }

【问题讨论】:

  • "@Url.Action("ChangePassword")", 看起来坏了
  • 我也使用这个网址:Home/ChangePassword
  • 但不工作
  • 您是要使用“GET”还是应该使用“POST”?
  • 没有。当我使用 POST 时不工作

标签: javascript jquery json model-view-controller


【解决方案1】:

我会建议从 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)

否则,将无法正常工作

【讨论】:

  • 函数被调用,但参数值没有传入 json 函数,如 Password、NewPassword、ConfirmPassword 显示 null。
  • User 类的外观如何?并尝试删除contentType: "application/json;charset=UTF-8",
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 2020-08-31
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多