zoukankan      html  css  js  c++  java
  • 用jquery实现无刷新登录

    html的部分

    <table> 
    <tr> 
    <td> 
    用户名: 
    </td> 
    <td> 
    <input type="text" id="username" /> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    密码: 
    </td> 
    <td> 
    <input type="text" id="password" /> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    验证码: 
    </td> 
    <td> 
    <input type="text" id="cord" /> 
    <img alt="点击更换验证码" title="看不清楚,请单击我!" id="checkcord" src="img.ashx" /> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <input type="button" onclick="login();" value="登录" /> 
    </td> 
    <td> 
    </td> 
    </tr> 
    </table> 

    jquery的部分

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>-----------别忘了引用这个链接,否则jquery不能用 
    <script type="text/javascript"> 
    //用jquery实现无刷新的登录验证 
    function login() { 
    $.ajax({ 
    url: 'Login.ashx', //访问路径 
    data: 'username=' + $("#username").val() + "&password=" + $("#password").val() + "&cord=" + $("#cord").val(), //需要验证的参数 
    type: 'post', //传值的方式 
    error: function () {//访问失败时调用的函数 
    alert("链接服务器错误!"); 
    }, 
    success: function (msg) {//访问成功时调用的函数,这里的msg是Login.ashx返回的值 
    alert(msg); 
    } 
    }); 
    } 
    //验证码图片 
    $(function () { 
    $("#username").focus(); 
    $("#checkcord").click(function () { 
    $("#checkcord").attr("src", "img.ashx?time=" + new Date()); 
    }); 
    }) 
    </script> 

    Login.ashx部分

    context.Response.ContentType = "text/plain"; 
    string username = context.Request.Form["username"]; 
    string password = context.Request.Form["password"]; 
    string cord = context.Request.Form["cord"]; 
    if (context.Session["cord"] != null) 
    { 
    if (context.Session["cord"].ToString() == cord) 
    { 
    if (username == "admin" && password == "admin") 
    { 
    context.Response.Write("登录成功!"); 
    } 
    else 
    { 
    context.Response.Write("登录失败!用户名和密码错误!"); 
    } 
    } 
    else 
    { 
    context.Response.Write("验证码错误!"); 
    } 
    } 
  • 相关阅读:
    树与堆
    Python基础
    python基础
    Flask基础知识
    其他(MySQL)
    发生错误:请确认您的电脑是否安装了excel软件,并且您的浏览器是否允许远行excel!具体操作请查阅帮助.
    idea启动项目,报java.lang.OutOfMemoryError: PermGen space 和启动项目很慢的问题解决
    安装jdk,tomcat,oracle,PL/SQL的一些问题
    linux下安装jdk8,nginx
    mybatic中xml新增一条数据获取自增id
  • 原文地址:https://www.cnblogs.com/lampon/p/3151501.html
Copyright © 2011-2022 走看看