Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Problema: usare più volte l' XmlHttpRequest

    Ciao a tutti,
    Sto sviluppando un'applicazione che usa AJAX-PHP, ho provato già a caricare un menù con l'oggetto XmlHttpRequest senza problemi, quando però riprovo ad utilizzarlo esattamente come la prima volta per caricare un'altro menù mi dà errore e non carica nulla (come terzo parametro della open dell'XmlHttpRequest gli lascio sempre true per fare la richiesta asincrona):

    0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]

    Se come terzo parametro della open dell'XmlHttpRequest gli dò invece false non mi dà più l'errore ma carica i dati solo per 1 secondo e poi spariscono.

    Allora la mia domanda principale è:
    E' possibile utilizzare più oggetti XmlHttpRequest o lo stesso oggetto XmlHttpRequest più volte per caricare più menù in una stessa pagina (nel mio caso è l'home page) ??
    Il mio problema è SOLO sulla seconda richesta che provo a fare con l'utilizzo dell' XmlHttpRequest.

  2. #2
    Se nn ho capito male era il mio stesso problema, che ho risolto cambiando il nome della variabile dell'oggetto GetXmlHttpObject().....ti lascio il mio script così capisci meglio

    codice:
    // AJAX: inizializzazione
    function GetXmlHttpObject()
    {
        var xmlHttp=null;
        try
          {
              // Firefox, Opera 8.0+, Safari, IE7
              xmlHttp=new XMLHttpRequest();
          }
        catch (e)
          {
              // Internet Explorer 6 e precedenti
              try
            		{
                 		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
              catch (e)
                {
                 		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
          }
        return xmlHttp;
    }
    
    // AJAX
    function menu1(){
    	var id = document.getElementById('xxx').value;
    							
    	xmlHttp1=GetXmlHttpObject() 
                  if (xmlHttp1==null)
                    {
                        alert ("Attenzione: Questo browser non supporta AJAX!");
                        return;
                    } 
                  var url="showMenu.php";
                  url=url+"?id="+id;
                  xmlHttp1.onreadystatechange=showMenu;
                  xmlHttp1.open("GET",url,true);
                  xmlHttp1.send(null);
    							
    }
    
    // AJAX
    function menu2(){
    	var id = document.getElementById('xxx').value;
    							
    	xmlHttp2=GetXmlHttpObject()
                  if (xmlHttp2==null)
                    {
                        alert ("Attenzione: Questo browser non supporta AJAX!");
                        return;
                    } 
                  var url="showMenu.php";
                  url=url+"?id="+id;
                  xmlHttp2.onreadystatechange=showMenu;
                  xmlHttp2.open("GET",url,true);
                  xmlHttp2.send(null);
    							
    }
    non so per quale ragione ma in questa maniera i menù mi si aggiornano regolarmente, mentre se uso lo stesso nome di variabile mi si visualizza solo il primo menù....bho!

  3. #3
    OK perfetto funziona, rimane solo un dilemma:

    Se faccio caricare entrambi i menù all' onLoad del body và tutto bene e vengono caricati come previsto.
    Se però faccio caricare un menù all' onLoad del body ed un menù all' onClick di un bottone (come servirebbe a me) il secondo menù mi dà problemi e non viene visualizzato.

    C'è un problema di sincronizzazione tra gli oggetti XmlHttpRequest dell' onLoad e onClick?

  4. #4
    non mi risulta...magari se posti il codice che carichi nel body e nel click magari ci potrebbe essere qualche errore di script....!!

  5. #5
    OK, allora di seguito il codice che carico dal body:

    //GLOBAL VAR DECLARATION
    var xhr=null; //Manage data
    var user;

    function load_tree(username){

    user=username;

    //check input fields
    if (user=='')
    {
    return false;
    }
    else {

    if (window.XMLHttpRequest)
    {// code for Firefox, Opera, IE7, etc.
    xhr =new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {// code for IE7, IE6, IE5
    xhr =new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xhr!=null)
    {

    try {

    var parameters="Menu.php?user="+user;

    xhr.onreadystatechange=Tree;

    xhr.open("GET",parameters,true);

    xhr.send(null);

    } catch(err){ alert(err); }

    }else { alert("Your browser does not support XMLHTTP."); }

    }

    }


    function Tree(){

    if (xhr.readyState==4) // 4 = "loaded"
    {
    if (xhr.status==200) // 200 = OK
    {
    var htmlCreate="";

    htmlCreate+="<Table id='table_menu' align='left' width='200' height='100' cellpadding='0' cellspacing='0' border='2' background='/tool_web/img/box.gif' bordercolor='white' rules='none' > ";

    var menu=document.getElementById('left_menu');

    var json_data=xhr.responseText;

    var tree=eval('('+json_data+')');

    var cont=0;

    for(i=0;i<tree.length;i++){

    htmlCreate+="<tr> <td width='10%'></td> <td width='30%' height='10' align='left'>
    <span style='CURSOR: pointer; font-weight: bold;' onclick=\"return content('"+tree[i]["NAME"]+"','"+user+"');\" /> [img]"+tree[i]["LINK_IMM"]+ "[/img] <td width='50%' height='10' align='left'>
    <span style='CURSOR: pointer; font-weight: bold;' onclick=\"return content('"+tree[i]["NAME"]+"','"+user+"');\" /> "+tree[i]["NAME"]+"</span></td></tr>";

    }

    htmlCreate+="<tr> <td>
    </td> </tr></Table>";

    menu.innerHTML=htmlCreate;

    return true;

    }
    else
    {
    alert("Problem retrieving data"+xhr.statusText);
    return false;
    }
    }else { document.getElementById('left_menu').innerHTML="Lo ading Left Menù.."; }

    }



    E di seguito quello che carico dall'onClick:

    //GLOBAL VAR DECLARATION
    var request=null; //Manage xml or json


    //LOAD TREE - BEGIN
    function cerca(user){

    if (window.XMLHttpRequest)
    {// code for Firefox, Opera, IE7, etc.
    request =new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {// code for IE6, IE5
    request =new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (request!=null)
    {

    try {

    var parameters="ProfileSub.php?user="+user+"&campo="+c ampo+"&valore="+valore;

    request.onreadystatechange=QueryProfile;

    //posso anche mettere a posto del php il file json o xml che sia
    request.open("GET",parameters,true);

    request.send(null);

    } catch(err){ alert(err); }

    }
    else
    {
    alert("Your browser does not support XMLHTTP.");
    }
    return true;
    }



    //LOAD TREE - END
    function QueryProfile(){

    //try {

    if (request.readyState==4) // 4 = "loaded"
    { alert("cazzo");
    if (request.status==200) // 200 = OK
    {
    var htmlCreate="";
    menu=document.getElementById('result_span');

    htmlCreate+="<tr> <td width='10%'></td> <td width='30%' height='10' align='left'> [img]/tool_web/img/menu.gif[/img]</td> <td width='50%' height='10' align='left'>

    </td> </tr>";

    var result=document.getElementById('result_span');

    var json_data=xhr2.responseText;

    var tree=eval('('+json_data+')');
    alert(tree);
    var cont=0;

    for(i=0;i<tree.length;i++){

    htmlCreate+="<tr> <td width='10%'></td> <td width='30%' height='10' align='left'>
    <span style='CURSOR: pointer; font-weight: bold;' onclick=\"return content('"+tree[i]["IMSI"]+"','"+user+"');\" /> [img]"+tree[i]["IMSI"]+ "[/img] <td width='50%' height='10' align='left'>
    <span style='CURSOR: pointer; font-weight: bold;' onclick=\"return content('"+tree[i]["IMSI"]+"','"+user+"');\" /> "+tree[i]["HEADER1"]+"</span></td></tr>";

    }

    htmlCreate+="<tr> <td> "+tree[1]["HEADER1"]+"cazzo
    </td> </tr>";

    menu.innerHTML=htmlCreate;

    return true;

    }
    else
    {
    alert("Problem retrieving data -"+request.status+"-"+request.statusText);
    }
    }
    // } catch(err){ alert(err); }
    }



    Il PHP:

    <html>
    <head>
    <title>Home - BSS Profiling</title>

    <link rel="stylesheet" href="css/tree.css"/>
    <link rel="stylesheet" href="css/scrollbar.css"/>
    <link rel="stylesheet" href="css/error.css"/>
    <link rel="stylesheet" href="css/normal.css"/>
    <link rel="stylesheet" href="css/table.css"/>

    <script language="JavaScript" src="js/menu.js"></script>
    <script language="JavaScript" src="js/query.js"></script>
    <script language="JavaScript" src="js/profile.js"></script>

    </head>
    <body bgcolor="#d2effc" onLoad="load_tree('<?php echo $username ?>');">

    ........

    <input type="submit" onClick="cerca('<?php echo $username ?>');" name="ricerca" value="Cerca" style="BACKGROUND-COLOR: #0066CC; font-family: Monotype Corsiva;font-size: 14pt; COLOR: white">

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.