What is Ajax?

Source: Internet
Author: User

What is Ajax?

AJAX (Asynchronous JavaScript and XML) is a new term, designed to describe two powerful features of JavaScript. These two properties have been overlooked by web developers for years, until recently Gmail, Google suggest and Google Maps's birth made people begin to realize its importance. These two neglected properties are:

* Request can be sent to the server without reloading the entire page.

* Parsing and processing of XML documents.

Step "Please!"---how to send an HTTP request

To send an HTTP request to the server with JavaScript, you need a class instance with this functionality.

Such classes are first introduced by Internet Explorer as ActiveX objects, known as XMLHTTP. Later Mozilla, Safari and other browsers followed suit, providing the XMLHttpRequest class, which supports the methods and properties provided by Microsoft's ActiveX objects.

Therefore, in order to create a cross browser class instance (object), you can apply the following code:

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}

(The previous example simplifies the code to explain how to create an instance of the XMLHTTP class.)

The actual code instance can be read in step 3 of this article.

If the server's response does not have an XML Mime-type header, some Mozilla browsers may not work correctly. To solve this problem, if the header of the server response is not text/xml, other methods can be invoked to modify the header.

Http_request = new XMLHttpRequest ();

Http_request.overridemimetype (' Text/xml ');

Next you decide what you need to do when you receive a response from the server. This requires telling the HTTP request object which JavaScript function to use to handle the response. You can set the object's onReadyStateChange property to the name of the JavaScript function to use. As shown below:

Http_request.onreadystatechange = nameofthefunction;

Note: There is no parentheses after the function name, and there is no need to pass parameters. There is also a way to define a function in the title page (fly) and the behavior it will take in response, as follows:

http_request.onreadystatechange = function(){
// do the thing
};

After defining how to handle the response, the request is sent. You can call the open () and send () methods of the HTTP request class, as follows:

Http_request.open (' Get ', ' http://www.example.org/some.file ', true);

Http_request.send (NULL);

* The first parameter of the Open () is the HTTP request method –get, POST, head or any server supported by the way you want to invoke

. This argument is capitalized according to the HTTP specification, otherwise some browsers, such as Firefox, may not be able to process the request. For more information about HTTP request methods refer to http://www.w3.org/Protocols/rfc2616/ Rfc2616-sec9.html Specs

* The second parameter is the URL of the requesting page. Because of its own security features, this page cannot be a third party domain Name page. At the same time must

To make sure that you use the exact domain name on all pages, calling open () gets the "permission denied" error prompt. A common mistake is to use domain.tld when visiting a site, but when you request a page, you use the www.domain.tld.* The third parameter sets whether the request is asynchronous. If true, the JavaScript function will continue to execute without waiting for the server to respond. This is "a" in "AJAX".

If the first argument is "POST", the parameter of the Send () method can be any data that you want to send to the server. The data is then sent to the server as a string, as follows:

Name=value&anothername=othervalue&so=on

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.