Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di daneel
    Registrato dal
    Oct 2002
    Messaggi
    229

    [JS] Utilizzo di XMLHttpRequest con file js esterni

    Sto provando l'uso dell'oggetto XMLHttpRequest attraverso il browser seguendo l'esempio presentato qui.
    Ho diviso il codice in due parti. Questa è la prima (dichiarazione della variabile inclusa):
    codice:
    var req;
    
    function loadXMLDoc(url) {
    	// branch for native XMLHttpRequest object
    	if (window.XMLHttpRequest) {
    		req = new XMLHttpRequest();
    		req.onreadystatechange = processReqChange;
    		req.open("GET", url, true);
    		req.send(null);
    	// branch for IE/Windows ActiveX version
    	} else if (window.ActiveXObject) {
    		req = new ActiveXObject("Microsoft.XMLHTTP");
    		if (req) {
    			req.onreadystatechange = processReqChange;
    			req.open("GET", url, true);
    			req.send();
    		}
    	}
    }
    
    function processReqChange() {
    	// only if req shows "complete"
    	if (req.readyState == 4) {
    		// only if "OK"
    		if (req.status == 200) {
    			// ...processing statements go here...
    			response = req.responseXML.documentElement;
    			method = response.getElementsByTagName('method')[0].firstChild.data;
    			result = response.getElementsByTagName('result')[0].firstChild.data;
    			eval(method + '(\'\', result)');
    		} else {
    			alert("There was a problem retrieving the XML data:\n" + req.statusText);
    		}
    	}
    }
    E questa la seconda, in pratica la sola funzione che lancia la richiesta al file lato server:
    codice:
    function checkName(input, response) {
    	if (response != "") {
    		// Response mode
    		message = document.getElementById("nameCheckFailed");
    		if (response == "1") message.className = "error";
    		else message.className = "hidden";
    	} else {
    		// Input mode
    		url = "XMLHttpRequest.asp?q=" + input;
    		loadXMLDoc(url);
    	}
    }
    Ho inserito la prima parte in un file js che ho incluso nella pagina html di prova, e la seconda direttamente nella pagina. Come mai separando in questo modo i frammenti di codice, e provando a dichiarare la variabile req o nel documento o nel js esterno, lo script non dà nessun risultato?

  2. #2
    Utente di HTML.it L'avatar di daneel
    Registrato dal
    Oct 2002
    Messaggi
    229
    up

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.