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

    [ajax]XMLHttpRequest e variabili

    Seguendo questa utile guida (http://antirez.com/articoli/spaghettiajax.html) ho creato delle funzioni ajax per fare delle richieste http.

    rispetto alle funzioni di esempio ho la necessità di passare una variabile alla funzione myHandler2 .
    (la variabile mi serve per passare il nome dell'id del div su cui vado a scrivere).

    io pensavo fosse una cosa semplice ma non funziona....
    provo a postare qui sotto le 3 funzioni se qualcuno ha voglia di darci un'occhiata.


    la prima è rimasta identica a quella del tutorial
    codice:
    var myRequest = null;
    
    function CreateXmlHttpReq2(handler) {
      var xmlhttp = null;
      try {
        xmlhttp = new XMLHttpRequest();
      } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
      xmlhttp.onreadystatechange = handler;
      return xmlhttp;
    }
    alla seconda ho aggiunto il passaggio della variabile "nomediv"

    codice:
    function myHandler2(nomediv) {
        if (myRequest.readyState == 4 && myRequest.status == 200) {
            e = document.getElementById(nomediv);
            e.innerHTML = myRequest.responseText;
        }
    }

    la terza funzione, quella che invia le variabili al php, l'ho modificata in base alle mie esigenze
    codice:
    function display(nome) {
        var num =document.forms[nome].num.value;
        var nomediv =document.forms[nome].nomediv.value;
        var url =document.forms[nome].url.value;
        var r = Math.random();
        myRequest = CreateXmlHttpReq2(myHandler2(nomediv));
        myRequest.open("GET","secondo.php?num="+escape(num)+"&&url="+escape(url)+"&&rand="+escape(r));
        myRequest.send(null);
    }


    L'errore che mi da' è questo myRequest has no properties

  2. #2

  3. #3
    Ciao.
    1) Handler non vuole parametri almeno gestito in queso modo
    2) L'oggetto xmlhttp in questo caso deve essere globale
    se no l'handler come lo vede ?

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4
    quello che temevo! ma non c'è da qualche parte un esempio di handler con parametri?
    cosa mi consigli di fare?

  5. #5
    Guarda se riesci ad adattare questo


    se vuoi passare parametri all'handler
    devi usare una funzione anomina tipo
    Codice PHP:
    req.onreadystatechange = function(){
                
    processReqChange(target);
            } 
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  6. #6
    grazie del consiglio e del link!

    ho risolto integrando l'handler con la funzione "specifica" che richiama il php. come consigliavi.

    ecco qui il codice finale funzionante


    codice:
    var myRequest = null;
    
    function CreateXmlHttpReq2() {
      var xmlhttp = null;
      try {
        xmlhttp = new XMLHttpRequest();
      } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
      
      return xmlhttp;
    }
    
    
    
        function display(nome,nomediv){
        
        var num =document.forms[nome].num.value;
        var url =document.forms[nome].url.value;
        var r = Math.random();
    
        myRequest = CreateXmlHttpReq2();
        var url="file.php?num="+escape(num)+"&&url="+escape(url)+"&&rand="+escape(r) ;
        myRequest.open("GET", url , true)
    	
    
       myRequest.onreadystatechange=function(){
    
    		if (myRequest.readyState==4 || myRequest.readyState=="complete"){
                    if (myRequest.status == 200){
    				document.getElementById(nomediv).innerHTML=myRequest.responseText;
    			} else {
    				document.getElementById(nomediv).innerHTML="ERRORE "+myRequest.status;
    			}
    		} // else document.getElementById(nomediv).innerHTML=myRequest.readyState; // debug
    	}
    	myRequest.send(null);
    }

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.