Using AJAX to get data asynchronously

Source: Internet
Author: User

Ajax is the name of the magic, I first saw it was also attracted by it, it is asynchronous JavaScript and XML, asynchronous JavaScript and XML, the introduction of Ajax on the internet introduced too much, I don't have much more, My eloquence is not good, not what they say so wonderful, can go to Http://zh.wikipedia.org/wiki/AJAX to see, here I just give everybody an example, understand how to use AJAX.

Ajax the most interesting place is that you can not refresh the page and can interact with the background, do not need to refresh the page to achieve a smooth transition between Web pages and Web pages. Using AJAX, you don't need to install a new plugin, just ask your browser to support JavaScript.

First you need your JavaScript object XMLHttpRequest, which is the core of Ajax, we all work around it, and we can get one from the following code:

if (window.XMLHttpRequest){
xmlObj = new XMLHttpRequest();
}else if (window.ActiveXObject){
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
}

We send the request to the service through the open method of this object,

The function is declared as Xmlhttprequest.open (string method, String URL, Boolean asynchronous);

Method is the way it is requested and can be used for get and post

URL is the resource you want to request

Asynchronous is a Boolean type, and True indicates that the interaction is set to asynchronous

xmlObj.open("GET",URL,true);
xmlObj.send("");

Calling Send () (parameter is null or NULL) will initiate a request.

For a get-way request, two times the same request will have the same result,

Because you want to set the interaction to asynchronous, you specify a callback function:

Xmlobj.onreadystatechange = callbackfunction;

The rest of the work is handled by the callback function.

One thing to note is that when you respond to an asynchronous request with a servlet or JSP,

The ContentType property to set the reponse indicates an XML format:

Response.setcontenttype ("Text/xml");

Everything's ready, so let's do a simple asynchronous request and display the result of the request return:

Client:

Send Request

Look here!

Here's data.jsp.

Zjcfan

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.