东莞上位机开发,东莞软件开发,软件定制,东莞软件开发,东莞APP开发定制,物联网APP定制开发,智能家居APP开发.小程序开发定制,商城开发,东莞软件开发公司,东莞软件公司,网站建设
东莞上位机开发,东莞软件开发,软件定制,东莞软件开发,东莞APP开发定制,物联网APP定制开发,智能家居APP开发.小程序开发定制,商城开发,东莞软件开发公司,东莞软件公司,网站建设

官方热线:0769-22501808

7x24热线:+86 13310811808

© 2012-2015 宏翔科技 版权所有

layui表单提交用户登录写法

宏翔科技技术文章2019/10/17 9:55:29


JS前端部份


    <script>
    $(function  () {
            layui.use('form', function(){             
             var form = layui.form;
              form.on('submit(login)', function (data) {
                  $.ajax({                    
                    url:'@Url.Action("Login")',                      
                    type:'POST',                      
                    data: JSON.stringify(data.field),                  
                      success:function(data){                          
                      console.log(data);                          
                      if (data.State) {
                              location.href = "@Url.Action("Index")"
                          }else{
                              layer.msg(data.Message)
                          }
                      }})                
                      return false;
              });
            });
})    
</script>

后端部份:

    public ActionResult Login()
        {          
          if(Request.IsAjaxRequest())
            {
                var param = HttpUtility.UrlDecode(Request.Form.ToString(), Encoding.UTF8);//前端表单数据转JSON字符串
                var JsonObj = new { username= "", password = "" };
                var RequestObj = JsonConvert.DeserializeAnonymousType(param, JsonObj);
                JsonReturnData jsonReturnData = new JsonReturnData();                
                if (DataContext.SystemUser.Where(a=>a.Accounts==RequestObj.username && a.Password==RequestObj.password).Any())
                {
                     jsonReturnData = new JsonReturnData() { State = true };
                }               
                 else
                {
                    jsonReturnData.Message = "登录失败,请检查您的用户密码";
                }
                return Json(jsonReturnData, JsonRequestBehavior.AllowGet);
            }
            return View();
        }