复用性代码之AJAX的创建

前端之家收集整理的这篇文章主要介绍了复用性代码之AJAX的创建前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

function checkUsername(){@H_502_1@

// 获得文件框值:@H_502_1@

var username = document.getElementById("username").value;@H_502_1@

// 1.创建异步交互对象@H_502_1@

var xhr = createXmlHttp();@H_502_1@

// 2.设置监听@H_502_1@

xhr.onreadystatechange = function(){@H_502_1@

if(xhr.readyState == 4){@H_502_1@

if(xhr.status == 200){@H_502_1@

document.getElementById("span1").innerHTML = xhr.responseText;@H_502_1@

}@H_502_1@

}@H_502_1@

}@H_502_1@

// 3.打开连接(防止缓存)@H_502_1@

xhr.open("GET","${pageContext.request.contextPath}/user_findByName.action?time="+new Date().getTime()+"&username="+username,true);@H_502_1@

// 4.发送@H_502_1@

xhr.send(null);@H_502_1@

}@H_502_1@

//创建异步请求对象@H_502_1@

function createXmlHttp(){@H_502_1@

var xmlHttp;@H_502_1@

try{@H_502_1@

xmlHttp=new XMLHttpRequest();@H_502_1@

}@H_502_1@

catch (e){@H_502_1@

try{// Internet Explorer@H_502_1@

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");@H_502_1@

}@H_502_1@

catch (e){@H_502_1@

try{@H_502_1@

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");@H_502_1@

}@H_502_1@

catch (e){}@H_502_1@

}@H_502_1@

}@H_502_1@


@H_502_1@

return xmlHttp;@H_502_1@

}@H_502_1@

猜你在找的Ajax相关文章