Summary Ajax Basics

Source: Internet
Author: User

Function getajax (){
// Create an XMLHttpRequest instance
VaR xhr = getxmlhttprequest ();
// Load the Open Method
Xhr. Open ("get", "index. jsp? Username = guoqianfang ", true );
/*
There are five parameters in the Open Method: method/URL/async/user/password. Three parameters are commonly used: method, URL, and async,
Method: Get and post. What is the difference between them? And & Symbol link cannot exceed 2 K; When a POST request
Adding parameters to a URL is safe, but can also be loaded to a URL and get the loaded parameters.
URL: It can be relative or absolute.
Async: true (asynchronous), false (synchronous, generally not used). The default value is true.

*/

Xhr. Send ();

/*
When a GET request is sent, you do not need to set xhr. setRequestHeader (header, value). The parameters of the send method can be written as null without writing anything,
If you want to write parameters in the send method, you must set xhr. setRequestHeader (header, value), which is a post Method Request by default.
The parameters in the send method cannot be obtained.
Xhr. setRequestHeader (header, value) must be set for post requests );

*/
// Write the call Function
Xhr. onreadystatechange = function (){
// Determine whether the call is complete
If (xhr. readystate = 4 ){
// Determine whether the server is successfully processed
If (xhr. Status = 200 ){
$ ("Content"). innerhtml = xhr. responsetext;
}
}
}
}
Function postajax (){
VaR xhr = getxmlhttprequest ();
Xhr. Open ("Post", "index. jsp", true );
Xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Xhr. Send ("username = guoqianfang ");
Xhr. onreadystatechange = function (){
// Determine whether the call is complete
If (xhr. readystate = 4 ){
// Determine whether the server is successfully processed
If (xhr. Status = 200 ){
$ ("Content"). innerhtml = xhr. responsetext;
}
}
}
}
Function $ (ID ){
Return document. getelementbyid (ID );
}
Function getxmlhttprequest (){
VaR xhr;
Try {
// IE browser
Xhr = new activexobject ("micorsoft. XMLHTTP ");
} Catch (error ){
Try {
// Firefox/operabrowser
Xhr = new XMLHttpRequest ();
} Catch (e ){
Return xhr;
}
}
Return xhr;
}

 

 

This is the simplest and most basic knowledge point of Ajax!

Related Article

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.