1. Home
  2. Computing & Technology
  3. JavaScript

Introduction to Ajax

2. Open Request

clr gif
Join the Discussion

Questions? Comments?

More of this Feature

XMLHttpRequest

Having created our AJAX object the next thing that we need to do is to initialize (or open) the object.

To be able to do this we need to know what server side process that we are going to call that will set return the requested information for us. For the purpose of this example we'll assume that the server side processing is written in PHP and will be in a file called myrequest.php but your server side processing can be written in whatever server side language you prefer. We start by assigning this to a field called url to make it easier to change later.

We can now open our request to the server by calling the open method that belongs to the AJAX object that we created in the first tutorial. This method takes three parameters. The first parameter is the request type ('GET' and 'POST' are the only ones supported by all browsers). The second parameter is the url of the server side process. The third parameter defines whether the call will by asynchronous (true) or synchronous (false).

The A in ajax stands for asynchronous and so that is the one we'll choose. Asynchronous means that the processing will continue on without waiting for the server side retrieval to complete whereas a synchronous call would stop all other processing and wait for the response from the server. Using an asynchronous call helps to hide the time that is taken for the server side retrieval.

var url = 'myrequest.php';
ajaxObj.open("GET", url, true);

This new code gets added after the code that we have already created.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.