Data exchange

In order to exchange data, the page must create an XMLHttpRequest object, which is a kind of intermediary between the user’s browser and the server. With XMLHttpRequest you can send a request to the server, and receive a response in the form of various kinds of data.

You can exchange data with the server in two ways. The first way is a GET request. In this request you address to the document on the server, passing it arguments through the URL itself. On the client side it makes sense to use Javascript’s escape function to prevent some data from interrupting the request.

It is not recommended to make GET-requests to the server with large amounts of data. That’s what POST-requests are for.

Client part, written in Javascript, must provide necessary functionality for secure data exchange with the server and provide methods for data exchange in any of the above mentioned ways. The server part should process the input data, and based on it generate new information (e.g. working with a database), and give it back to the client. For example, to request information from the server you can use the usual GET-request with the transfer of a few and small parameters, and to update the information or add new information will need to use POST-request, because it allows you to transfer large amounts of data.

As already mentioned, AJAX uses asynchronous data transfer. This means that while the data transfer is going on, the user can perform other actions he needs. During this time, should notify the user that the exchange of data, otherwise the user will think that something went wrong and may leave the site or re-call “hung” in his opinion, the function. Display during communication in a Web 2.0 application plays a very important role: visitors may not yet be used to this method of refreshing the page.

The response from the server can be more than just XML, as the name of the technology implies. In addition to XML, you can get a plain text response, or JSON (Javascript Object Notation). If the response was received as plain text it can be displayed immediately in a container on the page. When an XML response is received, the client usually processes the received XML document and converts the data into (X)HTML. If you get JSON response the client just has to execute received code (Javascript’s function eval) to get a full Javascript object. But you have to be careful and take into account the fact that malicious code can be transmitted using this technology, so you should check and process it carefully before executing the code received from the server. There is such a practice as “idle” request, in which no response from the server does not come, only the data on the server side is changed.

This object has different properties in different browsers, but in general it is the same.

Methods of XMLHttpRequest object
Note that method names are written in the same style (Camel-style) as the other Javascript functions. Be careful when using them.

abort() – cancel current server request.

getAllResponseHeaders() – get all response headers from the server.

getResponseHeader(“header_name”) – get the specified header.

open(“request_type”, “URL”, “asynchronous”, “user_name”, “password”) – initialize request to the server, specify request method. Type of request and URL are obligatory parameters. The third argument – boolean value. Usually it is always specified true or not specified at all (the default is true). The fourth and fifth arguments are used for authentication (it is very insecure to store authentication data in the script, as the script can be viewed by any user).

send(“content”) – send an HTTP request to the server and get a response.

setRequestHeader(“header_name”, “value”) – set request header values.