【问题标题】:ajax with return Json return blank page带有返回Json的ajax返回空白页
【发布时间】:2020-07-15 19:57:30
【问题描述】:

**嗨,我在一个视图中有登录和注册页面。登录控制器没有视图。我想在用户输入错误的电子邮件和密码时显示错误消息。但它返回带有 {"Fail"} 的空白页面,带有 url Account/Login

$(document).ready(function () {
$("#alertmessage").hide()
$("#logbut").on("click",function () {
    //collect the user data
    var data = {};
    data.Email = $("#Email").val();
    data.Password = $("#Password").val();
    var token = $('input[name="__RequestVerificationToken"]').val();

    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: "/Account/Login",
        type: "POST",
        dataType: "json",
        data: {
            model: data,
            __RequestVerificationToken: token,

        },
        success: function () {

                $("#alertmessage").show()


        },

        })
    })
})

 <div class="login-container">
 <div class="row">
<div class=" col-md-12 col-sm-12 col-12  login-form-1">
<h2>Login </h2>
<p>Log into your account</p>
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
<div id="alertmessage">
<div class=" alert alert-danger">
<span>
Email or Password is incorrect
</span>
</div>
</div>

@Html.AntiForgeryToken()

<div class="form-group col-md-12 col-sm-12 col-12">
<input id="Email" type="text" class="form-control" placeholder="Your Email *" value="" name="Emaillog" />
</div>
<div class="form-group col-md-12 col-sm-12 col-12 ">
<input id="Password" type="password" class="form-control" placeholder="Your Password *" value="" name="Passwordlog" />
</div>
<div class="form-group col-md-12 col-sm-12 col-12">
<input type="submit" class="btnSubmit" value="Login now" id="logbut" />
</div>
<div class="form-group col-md-6 col-sm-6 col-6">
<a href="#" class="ForgetPwd">Forget Password?</a>
</div>
}
</div>
</div>
</div>
[HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(string Emaillog,string Passwordlog)
    {

        string result = "Fail";

        User appUser = _context.Users.FirstOrDefault(u => u.Email == Emaillog);

        if (appUser != null)
        {
            if (appUser.Password ==Passwordlog)
            {
                Session["ActiveUser"] = appUser;
                return RedirectToAction("MainAccount");
            }
            }
        return Json(result, JsonRequestBehavior.AllowGet);    }

我尝试更改 JsonResult 操作,但还是一样。我不知道哪里出错了。 **

【问题讨论】:

    标签: ajax asp.net-mvc model-view-controller


    【解决方案1】:

    您的 ajax 调用未被调用,因为您的提交按钮正在提交表单并进行重定向。

    添加event.preventDefault() 按钮点击事件,使其不会执行正常的同步 POST。

    // add e parameter, this is for the event
    $("#logbut").on("click",function (e) {
        // this is to avoid the form being submitted, and proceed with your script call
        e.preventDefault();
    
        // ...
    })
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 2015-07-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多