PDA

View Full Version : [AJAX]


LeRoy
10-09-2007, 11:29
salve a tutti!
Da poco ho deciso di arricchire le mie pagine web con ajax e mi sono subito arenato su un problema;le funzioni javascript per la gestione in ajax sono queste:

function createXMLHttpRequest()
{
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function getNote(element) {
createXMLHttpRequest();
var url = "modules.php?op=modload&name=assistenza&file=note&key_note=" + escape(element.id);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleNote(element.id);
xmlHttp.send(null);
}


function handleNote(note_id)
{

if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
setNote(xmlHttp.responseXML);
}
}
}

function setNote(courseNote) {
var id_nota = courseNote.getElementsByTagName("id_nota")[0].firstChild.data;
var data_get = courseNote.getElementsByTagName("data")[0].firstChild.data;
//document.getElementById("div_3").innerHTML = xmlHttp.responseText;
alert("notA4:"+ id);
}

sostanzialmente quando l'utente clicca su una nota viene eseguita la funzione getNote alla quale viene passato un id e questo permette attraverso AJAX di fare una query su db e ricavare informazioni aggiuntive. Il problema è che non riesco a passare l'id immesso dall'utente alla funzione setNote: se eseguo il questo comando

function handleNote(note_id) {
alert("ID: "+note_id);
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){

setNote(xmlHttp.responseXML);
}
}

}

mi visualizza correttamente l'id,altrimenti se l'alert lo eseguo dentro l'if:

function handleNote(note_id) {

if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
alert("ID: "+note_id);
setNote(xmlHttp.responseXML);
}
}

}

non mi fa nulla:(

Crashbandy80
10-09-2007, 12:00
Ciao,
se fai un alert sullo status cosa visualizza?
Andando a memoria mi sembra che possa restituire sia 200 che 0, quindi se ricordo bene prova a modificare la condizione con

if (xmlHttp.status == 200 || xmlHttp.status == 0)

usernameIsAlreadyInUse
10-09-2007, 12:02
Lo status 200 te lo da se va a buon fine la richiesta, prova a mettere un else
e vedi se ti da il messaggio

function handleNote(note_id) {
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
alert("ID: "+note_id);
setNote(xmlHttp.responseXML);
}else{
alert("ERRORE");
}
}
}

usernameIsAlreadyInUse
10-09-2007, 12:32
Prova a togliere il parametro alla funzione che gestisce la risposta

LeRoy
10-09-2007, 12:38
Lo status 200 te lo da se va a buon fine la richiesta, prova a mettere un else
e vedi se ti da il messaggio

function handleNote(note_id) {
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
alert("ID: "+note_id);
setNote(xmlHttp.responseXML);
}else{
alert("ERRORE");
}
}
}


Facendo un po di prove ho scoperto che è if prima dello status 200 che non si verifica, ovvero se eseguo:

function handleNote(note_id)
{
if (xmlHttp.readyState == 4)
{

alert("ID: "+note_id);
if (xmlHttp.status == 200)
{

setNote(xmlHttp.responseXML);
}
}
}

non mi visualizza nulla :-(
se tolgo tutti i vari IF

function handleNote(note_id)
{
if (xmlHttp.readyState == 4)
{

alert("ID: "+note_id);
if (xmlHttp.status == 200)
{

setNote(xmlHttp.responseXML);
}
}
}

funziona. Se alla funzione handleNote non gli passo nulla e lascio i vari IF
CODE]
function handleNote()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{

setNote(xmlHttp.responseXML);
}
}
}
[/CODE]
il codice funziona ma non è corretto perchè io voglio passargli l'ID.

usernameIsAlreadyInUse
10-09-2007, 12:47
Visto che oggi nn avevo proprio niente da fare ti ho preparato questo esempio... dovrebbe fare al caso tuo, devi cambiare solo l'url e la gestione della risposta, però il grosso è fatto.

<html>
<head>
<script type="text/javascript">
var xmlhttp
var url="Test";


function getNote(obj){
xmlhttp=null
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}

if (xmlhttp!=null){
xmlhttp.onreadystatechange=function(){handleNote(obj);}
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}else{
alert("Il browser non supporta XMLHTTP.")
}
}


function handleNote(obj){
if (xmlhttp.readyState==4){
if (xmlhttp.status==200){
obj.innerHTML=xmlhttp.status + " " + xmlhttp.responseText
}else{
alert("Errore di connessione :" + xmlhttp.statusText)
}
}
}


</script>
</head>

<body>
<h2>HttpRequest</h2>
<p><b><a href="#" onclick="getNote(document.getElementById('A1'))">Nota:</a></b>
<span id="A1"></span>
</p>

<p><b><a href="#" onclick="getNote(document.getElementById('A2'))">Nota:</a></b>
<span id="A2"></span>
</p>
</body>
</html>

LeRoy
11-09-2007, 08:57
Visto che oggi nn avevo proprio niente da fare ti ho preparato questo esempio... dovrebbe fare al caso tuo, devi cambiare solo l'url e la gestione della risposta, però il grosso è fatto.

<html>
<head>
<script type="text/javascript">
var xmlhttp
var url="Test";


function getNote(obj){
xmlhttp=null
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}

if (xmlhttp!=null){
xmlhttp.onreadystatechange=function(){handleNote(obj);}
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}else{
alert("Il browser non supporta XMLHTTP.")
}
}


function handleNote(obj){
if (xmlhttp.readyState==4){
if (xmlhttp.status==200){
obj.innerHTML=xmlhttp.status + " " + xmlhttp.responseText
}else{
alert("Errore di connessione :" + xmlhttp.statusText)
}
}
}


</script>
</head>

<body>
<h2>HttpRequest</h2>
<p><b><a href="#" onclick="getNote(document.getElementById('A1'))">Nota:</a></b>
<span id="A1"></span>
</p>

<p><b><a href="#" onclick="getNote(document.getElementById('A2'))">Nota:</a></b>
<span id="A2"></span>
</p>
</body>
</html>





grazie mielle! adattando il tuo codice sono riuscito a farlo funzionare :D

usernameIsAlreadyInUse
11-09-2007, 10:43
Bene, poi mi dai il link cosi vedo come sta venendo ;)

LeRoy
11-09-2007, 14:44
Bene, poi mi dai il link cosi vedo come sta venendo ;)

mi piacerebbe molto darti il link perchè cosi avrei un parere da qualcuno che ci capisce parecchio ma il programma gira su una intranet aziendale :-(

usernameIsAlreadyInUse
11-09-2007, 14:49
;) Ok