Ajax learning dynamic Fetch, return the value of the server

Source: Internet
Author: User

<%@ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.