javascript写ajax代码

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

<html>

<head>

<title>Ajax一</title>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">

<scripttype="text/javascript">

functionsub(){

varxmlhttp;

if(window.XMLHttpRequest)

{//codeforIE7+,Firefox,Chrome,Opera,Safari

alert("1111");

xmlhttp=newXMLHttpRequest();

}

else

{//codeforIE6,IE5

xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.onreadystatechange=function()

{

if(xmlhttp.readyState==4&&xmlhttp.status==200)

{

alert("222");

document.getElementById("result").innerHTML=xmlhttp.responseText;

}

}

varurl="HelloServlet?user="+document.getElementById("user").value+"&date="+newDate();

xmlhttp.open("GET",url,true);

xmlhttp.send();

}

</script>

</head>

<bodystyle="text-align:center;">

<form>

<inputid="user"type="text"name="user"/>

<buttononclick="sub()">提交</button>

</form>

<divid="result"></div>

</body>

</html>

相关推荐