Добро пожаловать! Это — архивная версия форумов на «Хакер.Ru». Она работает в режиме read-only.
 

[РЕШЕНО] AJAX и XMLHttpRequest

Пользователи, просматривающие топик: none

Зашли как: Guest
Все форумы >> [Веб-программинг] >> [РЕШЕНО] AJAX и XMLHttpRequest
Имя
Сообщение << Старые топики   Новые топики >>
[РЕШЕНО] AJAX и XMLHttpRequest - 2010-08-29 13:15:28.866666   
lazzarek

Сообщений: 118
Оценки: 0
Присоединился: 2007-01-19 12:43:15.026666
Занялся изучением AJAX. Но некоторые примеры которые обращаются к серверу не работают. В качестве сервера использую набор XAMPP 1.5.4 установленный на виртуалку.
Один из таких пример привиден ниже. Почему может не работать?

Содержимое файлов

async.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>AJAX Foundations: Using XMLHttpRequest</title>
<sсriрt type="text/jаvаsсriрt" src="async.js"></sсriрt>
</head>
<bоdу оnlоаd="process()">
<p>Hello, server!</p>
<div id="myDivElement" />
</bоdу>
</html>

async.js
// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
// will store the reference to the XMLHttpRequest object
var xmlHttp;
// create the XMLHttpRequest object
try
{
// assume IE7 or newer or other modern browsers
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch(e) { }
}
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}

// performs a server request and assigns a callback function
function process()
{
// only continue if we have a valid xmlHttp object
if (xmlHttp)
{
// try to connect to the server
try
{
// initiate reading the async.txt file from the server
xmlHttp.open("GET", "async.txt", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
// change cursor to "busy" hourglass icon
document.bоdу.style.cursor = "wait";
}
// display the error in case of failure
catch (e)
{
alert("Can't connect to server:\n" + e.toString());
// revert "busy" hourglass icon to normal cursor
document.bоdу.style.cursor = "default";
}
}
}

// function that handles the HTTP response
function handleRequestStateChange()
{
// obtain a reference to the <div> element on the page
myDiv = document.getElementById("myDivElement");
// display the status of the request
if (xmlHttp.readyState == 1)
{
myDiv.innerHTML += "Request status: 1 (loading) <br/>";
}
else if (xmlHttp.readyState == 2)
{
myDiv.innerHTML += "Request status: 2 (loaded) <br/>";
}
else if (xmlHttp.readyState == 3)
{
myDiv.innerHTML += "Request status: 3 (interactive) <br/>";
}
// when readyState is 4, we also read the server response
else if (xmlHttp.readyState == 4)
{
// revert "busy" hourglass icon to normal cursor
document.bоdу.style.cursor = "default";
// read response only if HTTP status is "OK"
if (xmlHttp.status == 200)
{
try
{
// read the message from the server
response = xmlHttp.responseText;
// display the message
myDiv.innerHTML +=
"Request status: 4 (complete). Server said: <br/>";
myDiv.innerHTML += response;
}
catch(e)
{
// display error message
alert("Error reading the response: " + e.toString());
}
}
else
{
// display status message
alert("There was a problem retrieving the data:\n" +
xmlHttp.statusText);
// revert "busy" hourglass icon to normal cursor
document.bоdу.style.cursor = "default";
}
}
}


async.txt
Hello, client!

IE8 выдает: cannt conection server. Type error: отказано в доступе.
Opera пишет: There was a problem retrieving the data:
Post #: 1
RE: AJAX и XMLHttpRequest - 2010-08-29 13:42:08.756666   
lazzarek

Сообщений: 118
Оценки: 0
Присоединился: 2007-01-19 12:43:15.026666
Вопрос решился перестановкой на Denwer.
Post #: 2
Страниц:  [1]
Все форумы >> [Веб-программинг] >> [РЕШЕНО] AJAX и XMLHttpRequest







Связаться:
Вопросы по сайту / xakep@glc.ru

Предупреждение: использование полученных знаний в противозаконных целях преследуется по закону.