<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" >
Create a XMLHttpRequest Object
var xmlhttp;
function Createxmlhttp () {
Alert ("Come in");
Xmlhttp=new ActiveXObject ("msxml2.xmlhttp"); Not available in IE
Xmlhttp=new XMLHttpRequest (); Can all create XMLHttpRequest
return XMLHTTP;
}
Processing functions
Function Deal () {
Request status equals 4, response status = 200;
if (xmlhttp.readystate==4) {
if (xmlhttp.status==200) {
alert (xmlhttp.responsetext);//Get the result returned by the server;
var Msg=document.getelementbyid ("MSG")//Get Div object, the key is var defined variable, not add ";"
//And JavaScript is strictly case sensitive, so document.gete Lementbyid ("MSG"), must be written correctly
Msg.innerhtml=xmlhttp.responsetext; Show the results in Div
}else{
alert (xmlhttp.status);
}
}
}
Xmlhttp=createxmlhttp ();
alert (XMLHTTP);
function Send () {
Create a XMLHttpRequest Object
Xmlhttp=createxmlhttp ();
Binding State functions
Xmlhttp.onreadystatechange=deal;
Alert ("Come on");
var Age=document.getelementbyid ("Age"). Value//through ID, gets the value entered in input
var url= "http://localhost:8080/fujindehuodong/ajaxServer.jsp?age= "+age; Corresponding dynamic acquisition of parameters
Xmlhttp.open (' Get ', url,true);
Send a request;
Xmlhttp.send (NULL);
}
</script>
<body>
<div id= "MSG" ></div>
<br/>
<input type= "text" name= "Age" id= "age" ></input>
<input type= "button" value= "Send" onclick= "Send ()" ></input>
</body>
Ajax learning dynamic Fetch, return the value of the server