Autor Zpráva
Jack Finger
Profil
Dobrý den,

mám následující JS kód, který má za úkol vytvořit požadavek na server (na pozadí), kde předá souboru search_ajax.php proměnnou search (?search=data) a v ní data z <input>. jenže když to spustím, tak mi to píše Cannot connect to server, Cannot send data a Error reading response, invalid XML structure. když nechám vypsat xmlHttp tak mi to vypíše [object XmlHttpRequest] ale přitom podle konzoly js ve firefoxu (ve kterým to testuju) ta funkce createxmlhttprequest nic nevrátí. neví někdo co s tím?


// --- připojení ---
var xmlHttp = createXmlHttpRequest();
function createXmlHttpRequest(){
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","Microsoft.XMLHTTP");
for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++){
try {
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch(e){}
}
}
if(!xmlHttp){
alert('Chyba: Nelze vytvořit spojení AJAXu!!!');
}
else {
return xmlHttp;
}
}
// --- definice funkce get_this_search() ---
function get_this_search(){
if(xmlHttp){
try {
var get_search = document.getElementById('search').value;
if(!xmlHttp.open("GET","modules/search_ajax.php?" + get_search,true))
alert('Cannot open connection!');
xmlHttp.onreadystatechange = handleRequestStateChange;
if(!xmlHttp.send(null))
alert('Cannoct send data to server');
}
catch(e){
alert('Cannot connect server!'+e.toString());
}
}
}

function handleRequestStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
try {
handleServerResponse();
}
catch(e){
alert('Error reading response!
'+e.toString());
}
}
else {
alert('Retrieve data problem!'+xmlHttp.statusText);
}
}
}
function handleServerResponse(){
var xmlResponse = xmlHttp.responseXML;
if(!xmlResponse || !xmlResponse.documentElement){
throw("Invalid XML structure!");
}
var rootNodeName = xmlResponse.documentElement.nodeName;
if(rootNodeName == "parsererror"){
throw("Invvalid XML structure 2!");
}
xmlRoot = xmlResponse.documentElement;
idArray = xmlRoot.getElementByTagName("id");
stringArray = xmlRoot.getElementByTagName("string");
var html_out;
for(var i = 0; i < idArray.length; i++){
html_out += idArray.item(i).firstChild.data+","+stringArray.item(i).firstChild.dat a+"<br>";
}
join = document.getElementById("search_ajax_data");
join.innerHTML = html_out;
}
peta
Profil
http://www.volny.cz/peter.mlich/www.htm#mssub16
* jednoduchy priklad XMLHttpRequest (5k)
http://www.volny.cz/peter.mlich/www.htm#msub13
# PHP+MySQL+JSAjax minichat (14k) (POST)
# (zdroj: chatminiAjax.rar 5k)
Ale nemam to tak osetrene na chyby, jako je ten tvuj script a na disku mam novejsi verzi s nahradnim resenim v iframu.
Meldo
Profil
Chcelo by to aktualizovat. Tvoj priklad uz nejak nefunguje.
Toto téma je uzamčeno. Odpověď nelze zaslat.

0