Chciałem dzisiaj napisać skrypt AJaX, który pobiera z zewnętrznego serwera zawartość pliku XML i wyświetla go na stronie. Gdy ten plik XML jest u mnie na localhoście to wszystko działa. Jednak, gdy wrzucę go na mojego FTP'a (hatesz.xaa.pl) to już nie za bardzo.
ajax-create-object.js
Kod
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch(e) {
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP');
for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
try {
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) { }
}
}
if(!xmlHttp) {
alert("Wystąpił błąd podczas tworzenie obiektu XmlHttpRequest!");
}
else return xmlHttp;
}
function createXmlHttpRequestObject() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch(e) {
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP');
for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
try {
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) { }
}
}
if(!xmlHttp) {
alert("Wystąpił błąd podczas tworzenie obiektu XmlHttpRequest!");
}
else return xmlHttp;
}
ajax-get-message.js
Kod
function process() {
if(xmlHttp) {
try {
xmlHttp.open("GET","http://hatesz.xaa.pl/rozne/messages.xml",true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e) {
alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
}
}
}
function handleRequestStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
try {
handleServerResponse();
}
catch(e) {
alert("Błąd odczytu odpowiedzi: " + e.toString());
}
}
else {
alert("Pojawił się problem w uzyskaniu danych:\n" + xmlHttp.statusText);
}
}
}
function handleServerResponse() {
var xmlResponse = xmlHttp.responseXML;
xmlRoot = xmlResponse.documentElement;
titleArray = xmlRoot.getElementsByTagName("title");
contentArray = xmlRoot.getElementsByTagName("content");
var text = "";
for(var i = 0; i < titleArray.length; i++) {
text += titleArray.item(i).firstChild.data + ", " + contentArray.item(i).firstChild.data + "<br />";
}
message_box = document.getElementById("message_box");
message_box.innerHTML = text;
}
if(xmlHttp) {
try {
xmlHttp.open("GET","http://hatesz.xaa.pl/rozne/messages.xml",true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e) {
alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
}
}
}
function handleRequestStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
try {
handleServerResponse();
}
catch(e) {
alert("Błąd odczytu odpowiedzi: " + e.toString());
}
}
else {
alert("Pojawił się problem w uzyskaniu danych:\n" + xmlHttp.statusText);
}
}
}
function handleServerResponse() {
var xmlResponse = xmlHttp.responseXML;
xmlRoot = xmlResponse.documentElement;
titleArray = xmlRoot.getElementsByTagName("title");
contentArray = xmlRoot.getElementsByTagName("content");
var text = "";
for(var i = 0; i < titleArray.length; i++) {
text += titleArray.item(i).firstChild.data + ", " + contentArray.item(i).firstChild.data + "<br />";
}
message_box = document.getElementById("message_box");
message_box.innerHTML = text;
}
Błąd, jaki zwraca mi Firebug:
Cytat
206 Partial Content 235ms
Błąd parsowania XML: nie znaleziono elementu Obszar: moz-nullprincipal:{a51b0ae5-ed82-43d9-b0e6-7cec3251b7a0} Numer linii: 1, kolumna 1
Błąd parsowania XML: nie znaleziono elementu Obszar: moz-nullprincipal:{a51b0ae5-ed82-43d9-b0e6-7cec3251b7a0} Numer linii: 1, kolumna 1
Kodu strony z której wysyłam zapytanie nie daję ponieważ to chyba nie w nim leży problem ; )