.net采用ajax实现邮箱注册和地区选择实例

首先要知道Ajax是 Asynchronous JavaScript and XML(以及 DHTML 等)的缩写.

ajax就是在浏览器上同服务器实现异步交互。在XMLhttpRequest被广泛使用之前,用户停留在页面上没有办法实现局部更新的功能,只能通过刷新整个页面来获取最新的数据,而由此代码的代价是需要传输大量的数据,而且有可能临时的一些用户信息也会丢失,而ajax的使用实现了局部更新页面内容的作用,原理是调用XMLhttpRequest这个代理,向服务发送请求,之后通过ajax定义的处理接口来更新页面的内容。

接下来用ajax实现邮箱注册和地区选择实例来说明:

首先前台部分:

复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ajax的初步练习使用</title>
<style type="text/css">
div{width:800px;margin:0 auto;height:25px;}
</style>
   
<script type="text/javascript">
function createRequest()//创建对象
{
           var request;
           try
           {
                request = new XMLHttpRequest();
           }
           catch(microspft)
           {
                try
                {
                    request = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(othermicrosoft)
                {
                    try
                    {
                        request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(failed)
                    {
                        request = null;
                    }
                }
           }
           return request;
}
var req = null;//注册邮箱
function sendRequest()//发送请求
{
            if(document.forms[0].useremail.value=="")
            {
                alert("用户邮箱不可为空!");
                document.forms[0].useremail.focus();
                return false;
            }
            req = createRequest();//创建Ajax请求对象
            req.open("GET","default.aspx?Email="+document.forms[0].useremail.value);
            req.send("");//打开服务器连接,发送请求
            req.onreadystatechange = dealMethod;//设置服务器响应完成后要运行的函数
}
function dealMethod()//调用函数
{
            if(req.readyState==4&&req.status==200)//此时是服务器已经响应完成状态
            {
                if(req.responseText=="0")//responseText为服务器响应值属性
                    document.getElementById("canuse").innerHTML = "<img src='https://img.jbzj.com/file_images/article/201410/icon_need.gif' />"+"该邮箱已注册";
                else
                    document.getElementById("canuse").innerHTML = "<img src='https://img.jbzj.com/file_images/article/201410/icon_error.gif'/>"+"该邮箱未注册";
            }
}
var req2=null;//初始化下拉框
function GetSelect()
{
            req2 = createRequest();
            req2.open("GET","default.aspx?Selected=1");
            req2.send("");
            req2.onreadystatechange=changeSelected;
}
function changeSelected()
{
            if(req2.readyState==4&&req2.status==200)
            {
                var s = req2.responseText;
                var provinces = s.split('&')[0].split('|');//在后台返回字段中获得省份子列如(1,河南),(2,江西)等
                var cities = s.split('&')[1].split('|');//在后台返回字段中获得城市子列如(1,郑州),(2,洛阳),(3,开封)等
                document.forms[0].province.length=0;
                for(var i=0;i<provinces.length;i++)
                {
                    var op = new Option();
                    op.value = provinces[i].split(',')[0];
                    op.text = provinces[i].split(',')[1];
                    document.forms[0].province.options.add(op);//将省份编号和省份名分别以value和text的形式添加到select下的option里面
                }
                document.forms[0].city.length=0;
                for(var j=0;j<cities.length;j++)
                {
                    var op2 = new Option();
                    op2.value = cities[j].split(',')[0];
                    op2.text = cities[j].split(',')[1];
                    document.forms[0].city.options.add(op2);//将城市编号和城市名分别以value和text的形式添加到select下的option里面
                }
            }
}
var req3=null;//改变省份触动城市的改变
function GetCity()
{
            req3 = createRequest();
            req3.open("GET","default.aspx?ProId="+document.forms[0].province.value);
            req3.send("");
            req3.onreadystatechange=changeCity;
}
function changeCity()
{
            if(req3.readyState==4&&req3.status==200)
            {
                var s = req3.responseText;
                var cities = s.split('|');
                document.forms[0].city.length=0;
                for(var i=0;i<cities.length;i++)
                {
                    var op = new Option();
                    op.value = cities[i].split(',')[0];
                    op.text = cities[i].split(',')[1];
                    document.forms[0].city.options.add(op);
                }
            }
}     
</script>
</head>
<body>
    <form runat="server">
    <table>
    <tr>
    <th>Email</th>
    <th><input type="text" value=""/></th>
    <th></th>
    <th></th>
    </tr>
    <tr>
    <th><select onchange="GetCity();"></select></th>
    <th><select></select></th>
    <th><input type="button" value="注册"/></th>
    </tr>
    </table>
    </form>
</body>
<script type="text/javascript">
GetSelect();
</script>
</html>

然后是后台部分:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzgxp.html