Using AJAX to get data asynchronously

Source: Internet
Author: User
Tags object string window
Ajax| Data | asynchronous
"Guide" This article gives an example (using AJAX to get data asynchronously) and describes how to use Ajax.

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:// 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 (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 ("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:


Send Request

Look here!

Here's data.jsp.

<%response.setcontenttype ("Text/xml");%>



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.