Java Ajax Initialization

Source: Internet
Author: User

<script type= "Text/javascript" >
var http_request = false;
function Createxmlhttprequest () {
if (window. ActiveXObject) {//IE browser
Http_request = new ActiveXObject ("Msxml2.xmlhttp"); Create a XMLHttpRequest Object
}else if (window. XMLHttpRequest) {//Non IE browser
Http_request = new XMLHttpRequest (); Create a XMLHttpRequest Object
}
if (!http_request) {
Alert ("Cannot create XMLHttpRequest object instance! ");
return false;
}
}
function GetResult () {
var ResponseContext; Used to store response results returned from the server
if (http_request.readystate = = 4) {//Determine request status
if (Http_request.status = = 200) {//request succeeded, start processing return result
ResponseContext = Http_request.responsetext; Get the response content of the server
if (Responsecontext.indexof ("true")!=-1) {
Alert ("Congratulations! The user name is valid! ");
}else{
Alert ("Sorry! The user name has already been registered! ");
}
} else {//request page has errors
Alert ("The page you requested has an error! ");
}
}
}

function Checkusername (username) {
if (Username.value = = "") {
Alert ("Please enter your username!") ");
Username.focus ();
Return
} else {
Createxmlhttprequest ();
Http_request.onreadystatechange = GetResult; Call return result handler function
Http_request.open ("GET", "Checkuser?username=" +username.value, true); Create a connection to the server
Http_request.send (NULL); Send a request to the server
}
}
</script>

Example 2:

<script type= "Text/javascript" >var xmlHttp=false; function Createxmlhttprequest () {if(window. ActiveXObject) {xmlHttp=NewActiveXObject ("msxml2.xmlhttp.3.0"); }Else if(window. XMLHttpRequest) {xmlHttp=NewXMLHttpRequest ();            }} function Startrequest () {createxmlhttprequest (); Xmlhttp.onreadystatechange=Handlestatechange; Xmlhttp.open ("GET", "Http://localhost:8080/MyFirstWebPro/user.xml",false);        Xmlhttp.send (); } function Handlestatechange () {if(Xmlhttp.readystate = = 4){                if(Xmlhttp.status = = 200) {document.getElementById ("Results"). InnerHTML =Xmlhttp.responsetext; }Else{alert ("The page you requested has an error!" "); }            }        }    </script>

<body onload= "Startrequest (); >
<div id= "Results" ></div>

User.xml as follows

<?XML version= "1.0" encoding= "gb2312"?><Table>    <TR>        <th>Name</th>        <th>Age</th>        <th>Gender</th>    </TR>    <TR>        <TD>Wang Lili</TD>        <TD>22</TD>        <TD>Woman</TD>    </TR>    <TR>        <TD>Zhang Fang</TD>        <TD>22</TD>        <TD>Woman</TD>    </TR>    <TR>        <TD>Zhang Hui</TD>        <TD>22</TD>        <TD>Man</TD>    </TR></Table>

Java Ajax Initialization

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.