No page Jump and comparison using Ajax and jquery

Source: Internet
Author: User

Method 1:

<Script language = "JavaScript">

VaR xmlttp;

Function verify (){
// Obtain the text value in Dom Mode
VaR username = Document. getelementbyid ("username"). value;
// You need to create this object for IE and other browsers to write different code in different ways
If (window. XMLHttpRequest ){
// For IE7, 8, Firefox, opera, and mozillar
XMLHTTP = new XMLHttpRequest ();
// Fix bugs of javasillar in certain versions
If (XMLHTTP. overridemimetype ){
XMLHTTP. overridemimetype ("text/html ");
}
} Else if (window. activexobject ){
// For IE6, ie5.5, and ie5
// Two control names that can be used to create the XMLHTTPRequest object are saved in a JS array.
// Prefix the newer version
VaR activexname = ["msxml2.xmlhttp", "Microsoft. XMLHTTP"];
For (VAR I = 0; I <activexname. length; I ++ ){
Try {
// Take a control to create it. If it is successful, it will finally Loop
// If the creation fails, an exception is thrown, and the execution continues.
XMLHTTP = new activexobject (activexname [I]);
Break;
} Catch (e ){}
}
}
// Determine whether XMLHttpRequest is successfully created
If (! XMLHTTP ){
Alert ("XMLHTTPRequest object creation failed ");
} Else {
Alert (XMLHTTP );
}
// Register the callback function
XMLHTTP. onreadystatechange = callback;
// The first parameter sets the request method, get and post
// Set the URL of the second parameter
// The third parameter, "true", indicates synchronous interaction, and "false" indicates Asynchronous interaction.
XMLHTTP. Open ("get", "Servlet/testservlet? Username = "+ username, true );
// Send data
// In synchronous mode, send is executed only after the server data is returned.
// In asynchronous mode, send is executed immediately
XMLHTTP. Send (null );
}

Function callback (){
// Receives response data
// Determine whether the interaction is complete
If (4 = XMLHTTP. readystate ){
// Determine whether the interaction is successful
If (200 = XMLHTTP. Status ){
// Obtain the data returned by the server
// Obtain the plain text data output by the server
VaR responsetext = XMLHTTP. responsetext;

VaR divnode = Document. getelementbyid ("result ");
Divnode. innerhtml = responsetext;
}
}
}

</SCRIPT>

The second method is implemented using jquery.

<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.3.min.js"> </SCRIPT>

<Script language = "JavaScript">

Function verify (){
$. Get ("Servlet/testservlet? Username = "+ $ (" # username "). Val (), null, function (data ){
$ ("# Result" pai.html (data );
});
}

</SCRIPT>

Jquery may be much more convenient, but the principle should still be understood. Otherwise, it would be too big to change the framework!

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.