六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 45|回复: 0

学习ajax

[复制链接]

升级  76%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
38
 楼主| 发表于 2013-1-29 11:52:47 | 显示全部楼层 |阅读模式
ajax:

1、xmlHttpRequest对象
2、设置回调函数 onreadystatechange
3、新建连接 open
4、发送 send


function Ajax(){
};
Ajax.xmlHttpRequest = {};

Ajax.prototype.checkUserNameIsExist = function(obj){
var uname = obj.value;
if (!uname){
alert("用户名不能为空");
obj.focus();
return ;
}else{
Ajax.ajax(uname);
}
};


Ajax.prototype.ajax = function(uname){
// 发送请求到服务器,判断用户名是否存在
// 请求字符串
var url = "user.0905?method=judgeUserName&userName="+uname;
// 1. 创建XMLHttpRequest组件
this.xmlHttpRequest = this.createXmlHttpRequest();
// 2. 设置回调函数
this.xmlHttpRequest.onreadystatechange = this.invoke;
// 3. 初始化XMLHttpRequest组件
this.xmlHttpRequest.open("GET",url,true);//true为异步提交
// 4. 发送请求
this.xmlHttpRequest.send(null);
};


Ajax.prototype.createXmlHttpRequest = function(){
    if(window.ActiveXObject){
        return new ActiveXObject("Microsoft.XMLHTTP");
    }else if(window.XMLHttpRequest){
        return new XMLHttpRequest();
    }
};


Ajax.prototype.invoke = function(){
    if(Ajax.xmlHttpRequest.readyState == 4 && Ajax.xmlHttpRequest.status == 200){
        var name = Ajax.xmlHttpRequest.responseText;
        if (name == "true"){
        alert("用户名已经存在");
        }else{
    alert("用户名可以使用");
        }
    }
};
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表