• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

sunshinezjb

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

利用ajax进行post传值,登录QQ和密码代码

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <script src="js/jquery-1.10.2.js"></script>
  <script src="js/WebForm1.js"></script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <table>
        <tr><td>QQ:</td><td><input type="text" id="txtQQ" /></td></tr>
        <tr><td>密码:</td><td><input type="password" id="txtPwd" /></td></tr>
        <tr><td><input type="button" value="登录" id="btnLogin" /></td></tr>
      </table>
    </div>
  </form>
</body>
</html>

js部分:

$(function () {
   $("#btnLogin").click(function () {
      var qq = $.trim($("#txtQQ").val());
      var pwd = $.trim($("#txtPwd").val());
      if (qq == "" || pwd == "") {
         alert("qq或密码不能为空");
      } else {
         $.post("../ajax/Handler1.ashx", { "qq": qq, "pwd": pwd }, function (data) {
             switch (data) {
                 case "1":
                     alert("登录成功");
                     break;
                 case "2":
                     alert("QQ或者密码错误");
                     break;
                 case "3":
                     alert("程序异常");
                     break;
               };
          });
       };
   })
})

后台:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace exercise_20180305.ajax
{
   /// <summary>
   /// Handler1 的摘要说明
   /// </summary>
   public class Handler1 : IHttpHandler
   {
       string connStr = 
       ConfigurationManager.ConnectionStrings["sq_ruanmou"].ToString();
       SqlConnection con = null;
       SqlCommand cmd = null;

       public void OpenDB() {
         con = new SqlConnection(connStr);
         con.Open();
       }
       public void ProcessRequest(HttpContext context) {
          try{
              string qq = context.Request.Form["qq"];
              string pwd = context.Request.Form["pwd"];

              string sql = "select count(*) from UserInfor where QQ=@QQ and Pwd=@Pwd";
              SqlParameter[] para = new SqlParameter[]{
              new SqlParameter("@QQ",qq),
              new SqlParameter("@Pwd",pwd)
           };
           OpenDB();
           using (cmd = new SqlCommand(sql, con)) {

               foreach (var p in para) {
                   cmd.Parameters.Add(p);
               };
               if (Convert.ToInt32(cmd.ExecuteScalar().ToString()) > 0) {
                  context.Response.Write("1");
               }else{
                   context.Response.Write("2");
               }
          }
     }
     catch (Exception ex){
         context.Response.Write("3");
      }
   }

    public bool IsReusable {
        get {
            return false;
         }
     }
  }
}

 

posted on 2018-03-09 23:17  sunshinezjb  阅读(119)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3