Processing of the answer


In the previous example we have made function of search to the server. But she, as a matter of fact, is unsafe, as we do not process a status of object and a status of the answer from the server.


Let's add our code that he could deduce  the visual notification about process of loading.



...

Request.onreadystatechange = function ()

{

// If data exchange is completed

if (Request.readyState == 4)

{

// We pass management obrabotchiku the user

r_handler (Request);

}

else

{

// We notify the user on loading

}

}

...



As you already know, object XMLHttpRequest allows to learn{find out} the status of the answer from the server. We shall use this opportunity.



...

Request.onreadystatechange = function ()

{

// If data exchange is completed

if (Request.readyState == 4)

{

if (Request.status == 200)

{

// We pass management obrabotchiku the user

r_handler (Request);

}

else

{

// We notify the user on the occurred mistake

}

}

else

{

// We notify the user on loading

}


}

...



Variants of the answer from the server


From the server it is possible to obtain the data of several kinds:

1.         The usual text

2.         XML

3.         JSON


If you receive the usual text you can direct at once it  to the container, that is on a conclusion. At data acquisition as XML you should process the data with the help of DOM-functions, and present result with help HTML.


JSON is objective notation Javascript. With its  help it is possible to present object as a line (here it is possible to result analogy to function of serialization). At reception of the JSON-given you should execute them to receive high-grade object Javascript and to make with it  necessary operations. Remember, that such data transfer and performance of them are not safe. You should watch{keep up} that, that acts on execution .


Example of code JSON:



{


"data":

{

"misc":

[

{

"name": " a JSON-element one ",

"type": " the Subtitle 1 "

},


{

"name": " a JSON-element two ",

"type": " the Subtitle 2 "

}

]

}


}


At reception of such code, we make the following action:



var responsedata = eval ("(" + Request.responseText + ")")


After performance of the given code the object responsedata will be accessible to you.


Job with server programming languages


Such job differs nothing from usual. For examples I shall take PHP as server language. In a client part has changed nothing, but the server part now is submitted by a PHP-file.


By tradition, we shall start with a greeting to our remarkable world:



echo " Hello, World! ";


At the reference{manipulation} to this file to the client line Hello, World will return. As you understand, it represents the broadest opportunities for construction of applications. On the basis of transfer of arguments by a call of the server with help XMLHttpRequest it is possible to make parametrization of a conclusion, thus having provided extensive functionality of the Web-application.


Besides PHP, it is possible to use any other server programming language.


Clause{Article} is written with the purpose to tell to newbies about job AJAX from within. If it seems to you, that something is written is inexact or it is incorrect, correct, please, for me to create worthy clause{article} together.

Certainly, if there is an opportunity, it is necessary to use existing frejmvorki, I think. But to know " as it works " it is necessary.