谁有比较简单的AJAX代码 实例

来源:百度知道 编辑:UC知道 时间:2024/06/28 02:42:18
谁有比较简单的AJAX代码 实例,比如注册时验证用户名是否存在 PHP 版的 给我发一个 谢了~
1楼的if(str==1){
alert("存在");
}else{
alert("不存在");
} 根本不对啊

js 部分
//生成Ajax的调用对象
function creatAjax()
{
var HttpRequest=false;
try {
HttpRequest=new XMLHttpRequest();
} catch(e) {
var arrXMLHTTP=["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i<arrXMLHTTP.length;i++) {
try {
HttpRequest=new ActiveXObject(arrXMLHTTP[i]);
} catch(e) {}
if(HttpRequest) break;
}
}
return HttpRequest;
}

//使用ajax
function useAjax(username){
var ajax = creatAjax();
strUrl = "login.php"
ajax.open("POST",strUrl,false);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
strPost="username="+username;
ajax.send(strPost);
var str = ajax.responseText;
if(str==1){
alert("存在");
}else{
alert("不存在&quo