ajax学习

最近这几天一直在学习ajax,其实ajax就是那几个东西。

1.创建异步的请求javaScrict 类入下代码:

//根据浏览器不同创建相应的异步对象
function createXmlHttp() {
    request = false;
    try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
        //alert("here");
    } catch (e) {
        try {
           
            request = new ActiveXObject("Microsoft.XMLHTTP");
           
        } catch (e2) {
            request = false;
        }
    } 
          if (!request && typeof XMLHttpRequest != 'undefined') {
              request = new XMLHttpRequest();
          }
    return request;
}

2.将数据发送到服务器,有两种请求方式。

(1)get方式,提交时不带任何参数,如下代码:

      function getData() {
             var xmlrequest = createXmlHttp();
             xmlrequest.open("GET", "FirstAjaxForm.aspx", true);
         xmlrequest.onreadystatechange = function () {    //当服务器的状态改变时
              if (xmlrequest.readyState == 4) {          //服务器已经完全数据的传送
                  if (xmlrequest.status == 200) {        //服务器发送回来的 数据是正常的
                      alert(xmlrequest.responsText);
                  }
              }
              //alert("now");
          }
             xmlrequest.send(null);                  //请求服务器

         }
  (2)post方式,提交时要穿参数。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LOGIN.aspx.cs" Inherits="WebApplication2.LOGIN" %>

<!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></title>
    <script type="text/javascript" src="common/common.js"></script>
    <style type="text/css">
     .table
     {
     	border:0px;
     }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            getElement("login").onclick = doLogIn;
            xhr = createXmlHttp();
        }

        //用户点击登陆按钮
        function doLogIn() {
            var name = getElement("txtName");
            var password = getElement("txtPassword");
            if (name.value == "") {
                alert("请输入用户名");
                name.focus();
            }
            else if (password.value == "") {
                alert("请输入密码");
                password.focus();
            }
            else {
                valideInput(name, password)
                
            }
        }

        //用户登陆
        function valideInput(txtName,txtpassword) {
            var name = txtName.value;
            var txtpassword = txtpassword.value;
            xhr.open("post", "LOGIN.aspx", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var res = xhr.responseText;
                        var json = eval('(' + res + ')');
                        switch (json.status) {
                            case "1":
                                alert("验证不通过");
                                break;
                            case "0":
                                alert("验证通过");
                                break;
                            default:
                                alert("发生未知错误");
                                break;
                        }
                    }
                }
            }
            xhr.send("isPostback=1&txtName=" + name + "&txtPassword=" + txtpassword);
        } 
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table class="table" >
    <tr>
    <td>用户名</td>
     <td>
         <input id="txtName" type="text" /></td>
    </tr>

     <tr>
    <td>密码</td>
     <td>
         <input id="txtPassword" type="password" /></td>
    </tr>

     <tr>
    <td>
        <input id="login" type="button" value="登陆"  οnmοusedοwn="doLogIn()"/></td>
     <td>
         <input id="Button2" type="button" value="注册" /></td>
    </tr>
    </table>
    
    </div>
    </form>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值