Basic Ajax knowledge, suitable for beginners

Source: Internet
Author: User

I have always wanted to learn Ajax, and I feel that it can improve the user experience and make better network applications.ProgramSo I found some relevant information online and got a preliminary understanding of Ajax ~

Ajax (Asynchronous JavaScript and XML) is a new term designed to describe the powerful performance of JavaScript. these two performances have been ignored by network developers for many years. Until recently, the birth of Gmail, Google suggest, and Google Maps made people begin to realize their importance.

The two neglected performances are:
You can send a request to the server without reloading the entire page.
Parse and process XML documents.

Step 1-"Please! "--- How to send an HTTP request

To use JavaScript to send an HTTP request to the server, a class instance with this function is required. this class is first introduced by Internet Explorer as an ActiveX object, called XMLHTTP. later, Mozilla, Safari, and other browsers followed suit and provided the XMLHttpRequest class, which supports the methods and Attributes provided by Microsoft ActiveX objects.

Therefore, to create a cross-browser class instance (object), you can apply the followingCode:

If (window. XMLHttpRequest) {// Mozilla, Safari ,...
Http_request = new XMLHttpRequest ();
} Else if (window. activexobject) {// IE
Http_request = new activexobject ("Microsoft. XMLHTTP ");
}

(The code is simplified in the previous example to explain how to create an XMLHTTP class instance. For the actual code instance, see Step 3 .)

Some Mozilla browsers may not work properly if the server's response does not contain the XML mime-type header. to solve this problem, if the server response header is not text/XML, you can call other methods to modify the header.

Http_request = new XMLHttpRequest ();
Http_request.overridemimetype ('text/xml ');

Next, we need to decide what to do when we receive the response from the server. this tells the HTTP request object which JavaScript function is used to process the response. you can set the onreadystatechange attribute of the object to the name of the JavaScript function to be used, as shown below:

Http_request.onreadystatechange = nameofthefunction;

Note: There are no parentheses after the function name and no parameters need to be passed. There is also a way to define the function and its actions for the response in the response page (FLY), as shown below:

Http_request.onreadystatechange = function (){
// Do the thing
};

After defining how to handle the response, you need to send the request. You can call the open () and send () Methods of the HTTP request class, as shown below:

Http_request.open ('get', 'HTTP: // www.example.org/some.file', true );
Http_request.send (null );

the first parameter of open () is the HTTP Request Method-Get, post, head or the method you want to call supported by any server. according to HTTP specifications, this parameter must be capitalized; otherwise, Some browsers (such as Firefox) may not be able to process requests.

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.