[AJAX] Retour "undefined" d'une variable
Bonjour,
Je ne comprends pas ce qui cloche.
la variable libelle_service est "undefined".
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
function validation_compte () {
.....
libelle_service = lib_service(id_service);
.....
}
function lib_service (id_service) {
getXhr();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
};
xhr.open("POST","Script/BDD/libservice.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-15');
xhr.send("id_service=" + id_service);
} |
La fonction lib_service renvoie bien ce qu'il faut (si je mets un alert(xhr.responseText);, je vois le bon résultat).
Si je mets
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
function lib_service (id_service) {
return 'coucou1';
getXhr();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
};
xhr.open("POST","Script/BDD/libservice.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-15');
xhr.send("id_service=" + id_service);
} |
OU
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
function lib_service (id_service) {
getXhr();
return 'coucou1';
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
};
xhr.open("POST","Script/BDD/libservice.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-15');
xhr.send("id_service=" + id_service);
} |
C'est bon aussi.
Si je mets
Code:
1 2 3 4
|
if(xhr.readyState == 4 && xhr.status == 200) {
return 'coucou';
} |
ça va pas non plus.
Mais je ne vois pas pour quelle raison.