Example Ajax request - Design and development of web-applications

Example Ajax request

A general example of a page using Ajax technology is shown below:

& lt;! DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN & gt;

& lt; html & gt;

& lt; head & gt;

& lt; title & gt; Ajax & lt;/title & gt;

& lt; meta http-equiv = Content-Language content = en & gt;

& lt; meta http-equiv = "Content-Type '' content = '' text/html; UTF-8 '' & gt;

& lt; script type = text/javascript "src = '' request.js &&; & lt;/script & gt;

& lt;/head & gt;

& lt; body onclick = '' http_zapros () & nbsp;

Click anywhere, please. & lt; br/& gt;

& lt; div id = otvet /& gt;

& lt;/body & gt;

& lt;/html & gt;

The request.js file contains the following script:

// declare and create an XMLHttpRequest object

var xmlHttp = createXmlHttpRequestObject ();

// The function to create an XMLHttpRequest object

function createXmlHttpRequestObject () {var xmlHttp;

try {// if the browser is Firefox, Opera 8.0+, Safari

xmlHttp = new XMLHttpRequest ();

}

catch (e) {// if the browser is Internet Explorer

try {xmlHttp = new ActiveXObject ( Msxml2.XMLHTTP );}

catch (e) {// earlier versions of the browser

try {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");}

catch (e) {

alert ("Bam browser does not support Ajax!"); return false;

}}}

return xmlHttp;

}

// Send Asynchronous HTTP Request Function

function http_zapros () {

if (xmlHttp) {// if the object is created

try {// Trying to send a request to the server

// create a request to receive the request.txt file from the server

xmlHttp.open ("GET", "request.txt", true);

// set the function - the state change handler

xmlHttp. onreadystatechange = obrabotka; xmlHttp.send (null);

}

// Error message in case of failure

catch (e) {alert ( He can not connect to the server );}

}}

// Server Response Processing Function

function obrabotka () {

if (xmlHttp.readyState == 4) {

// Only with code 200 OK processed data

if (xmlHttp.status == 200) {

try {

response = xmlHttp.responseText; // Read server message

// Looking for a place on the page where we will write the server response

myDiv = document.getElementByldfotvet );

myDiv.innerHTML + = response;

}

catch (e) {ale | 1 ("Error while reading the answer");}

}

else {// Output the message about the status of the answer

alle ("There was a problem retrieving data from the server:" + "

xmlHttp.statusText);

thematic pictures

Also We Can Offer!

Ошибка в функции вывода объектов.