日期:2014-05-16  浏览次数:20598 次

Ajax异步提交

Ajax 异步提交的简历完全在于存在一个XmlHttpServlet的创建,

通过xmlHttp来发送数据请求 ,一般用到的有.get(),.post(),.getJSON(),.ajax(),loda()

创建xmlHttp对象方法

?

function createXMLHttpRequest() {?
??? ?if (window.ActiveXObject) {?
??????? ?xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");?
???? ?}??
???? ?else if (window.XMLHttpRequest) {?
??????? ?xmlHttp = new XMLHttpRequest();?
???? ?}?
??}

?

之后还要写一个对请求处理的

?

function process(){??
??????? if(xmlHttp.readyState == 4){??
??????????? if(xmlHttp.status == 200){
??????????? document.getElementById("userSpan").innerHTML = xmlHttp.responseText;??
??????????? }??
??????? }??
??? }

如果状态是4并且协议等于200,那么说明一个完整的请求完成

?

?

然后就是调用

? function checkUsername(){
? createXMLHttpRequest();
? var url ="servlet/ServletPort?username="+escape(document.userForm.elements("username").value);??
??????? xmlHttp.open("POST",url,true);??
??????? xmlHttp.onreadystatechange = process;??
??????? xmlHttp.send(null);
? }

?

通过.open("post/get",url,true)来发送请求

?

?

?