Ajax使用POST方式异步提交数据

实现异步用户注册

一、使用HttpHandler实现服务器代码:

<%@ WebHandler Language="C#" Class="RegisterAjax" %>

using System;
using System.Web;
using BookShopBLL;
using BookShopModels;

public class RegisterAjax : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        try
        {
            string mess = "";
            context.Response.ContentType = "text/plain";
            Users user = new Users();
            
            //获取用户名,注意这里使用Request.Form[]
            user.LoginId=context.Request.Form["loginId"].ToString();
            //获取密码,注意这里使用Request.Form[]
            user.LoginPwd = context.Request.Form["loginPwd"].ToString();
            user.Name = context.Request.Form["name"].ToString();
            user.Address = context.Request.Form["address"].ToString();
            user.Phone = context.Request.Form["phone"].ToString();
            user.Mail = context.Request.Form["mail"].ToString();

            UserRoles ur = new UserRoles();
            ur.Id = 2;//会员
            UserStates us = new UserStates();
            us.Id = 1;//1.正常 2.无效

            user.UserRole = ur;
            user.UserState = us;

            if (UserManager.Add(user))
            {
                mess = "注册成功!";
            }
            else
            {
                mess = "注册失败!";
            }
            context.Response.Write(mess);
        }
        catch (Exception)
        {
            context.Response.Write("注册失败!");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

二、页面 javascript 代码:


                    
                    

你可能感兴趣的:(Asp.Net,Ajax)