今天教你使用Jquery调用Ajax提交请求。验证用户登录,这里一定要写登录,不要写登陆,这是从吉日嘎啦(是个大师级人物)那里得知的。不废话了,先看Controller
public ActionResult Login(FormCollection form)
                {
                         string result = "no";
                         if (Request.IsAjaxRequest())
                        {
                                 string name = form[ "name"];
                                 string pwd = form[ "pwd"];
                                
                                 if (name.Equals( "admin") && pwd.Equals( "admin"))
                                {
                                        result = "ok";
                                        HttpCookie cookie = new HttpCookie( "user");
                                        cookie.Value = name;
                                        Response.Cookies.Add(cookie);
                                }
                                Response.CacheControl = "no-cache";
                        }
                         return Content(result);
                }
这段我只解释最后一句,Content(result)返回的是一个文本内容,也就是个字符串。
我们再看看页面的代码
< asp:Content ID ="Content1" ContentPlaceHolderID ="TitleContent" runat ="server" >    
                Index    
asp:Content>    
< asp:Content ID ="Head" ContentPlaceHolderID ="HeadContent" runat ="server" >    
                 < script language ="javascript" type ="text/javascript" src ="../../../jquery-1.3.2.min.js" > script>    
                 < script type ="text/javascript" >    
             function cookieInfo() {    
                    var cookie = document.cookie;    
                    var name = "";    
                    if (cookie != "") {    
                       var begin = cookie.indexOf('=') + 1;    
                       name = cookie.substring(begin, cookie.length);    
                                         }    
                                   return name;    
                                }    
                   $(function () {    
                       if (cookieInfo() == "") {    
                                  $("#login").show();    
                                 $("#logInfo").hide();    
                                                }    
                       else {    
                               $("#login").hide();    
                               $("#logInfo").show();    
                             }    
                            $("#login").ajaxSend(function () {    
                               $(this).hide();    
                               $("#showLog").show();    
                               $("#showLog").html("请等待...");    
                                            });    
                   $("#btnLogin").click(function () {    
          $.post("/System/Code/Login", { name: $("#txtName").val(), pwd: $("#txtPwd").val() },    
              function (data) {  $("#login").show();    
                       $("#showLog").hide();    
 if (data.indexOf("ok") != -1)   {  var name = cookieInfo();    
    $("#login").html("欢迎你:" + name);    
 }    
else { if ($("#login").html().indexOf("用户名或密码错误") == -1)                                                                                                                     $("#login").prepend("用户名或密码错误 < br >");    
                              }    
                       });    
              });    
      }); 
     script>
asp:Content>
< asp:Content ID ="Content2" ContentPlaceHolderID ="MainContent" runat ="server" >
         < div id ="login" >
                用户名: < %=Html.TextBox("t1", "", new { id = "txtName" }) % > < br />
                密码:
                 < %=Html.TextBox("t2", "", new { id = "txtPwd" })% > < br />
                 < input type ="button" value ="登陆" id ="btnLogin" />
         div>
         < div id ="showLog" >
         div>
         < div id ="logInfo" >
                欢迎你: < %=this.Request.Cookies["user"]==null?"":Request.Cookies["user"].Value% >
         div>
asp:Content>
asp.net MVC2 初探十二_第1张图片