学习ajax
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("用户名可以使用");
}
}
};
页:
[1]