Probleme ajax

vivi86 Messages postés 5 Statut Membre -  
 Vivi86 -
Bonjour, je débute en ajax et donc j'essaye de faire un petit bout de programme mais je n'arrive pas à voir d'où vient mon erreur si quelqu'un pouvait me donner un petit coup de pouce se serait sympa.

Le but du programme est tout simplement d'afficher "Coucou ok" ou "Coucou pas ok"

testajax.html
<html>
<head>
<title>Test AJAX - XMLHttpRequest</title>

<script type="text/javascript">
<!--
function EnvoiRequete(FonctionCallback) {
if (xhr && xhr.readyState != 0) {
xhr.abort();
}

xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
FonctionCallback(xhr.responseText);
}
};

xhr.open("GET", "tstreponseajax.php?param=oui", true);
xhr.send(null);
}

function LireReponse(LaReponse) {
document.getElementById("reponse").appendChild(LaReponse);
}
//-->
</script>
</head>

<body>
<p>
<button onclick="EnvoiRequete(LireReponse);">Afficher coucou</button>
<div id="reponse"></div>
</p>
</body>
</html>

tstreponseajax.php

<?php

header("Content-Type: text/plain"); // Utilisation d'un header pour spécifier le type de contenu de la page. Ici, il s'agit juste de texte brut.

$param = (isset($_GET["param"])) ? $_GET["param"] : NULL;

if ($param == 'oui') {
// Faire quelque chose...
echo "Coucou OK";
} else {
echo "Coucou pas OK";
}

?>

2 réponses

atspinto Messages postés 338 Date d'inscription   Statut Membre Dernière intervention   27
 
testajax.html
<html>
<head>
<title>Test AJAX - XMLHttpRequest</title>

<script type="text/javascript">
<!--
function EnvoiRequete(FonctionCallback) {
xhr = new XMLHttpRequest();
if (xhr && xhr.readyState != 0) {
xhr.abort();
}

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
FonctionCallback(xhr.responseText);
}
};

xhr.open("GET", "tstreponseajax.php?param=oui", true);
xhr.send(null);
}

function LireReponse(LaReponse) {
document.getElementById("reponse").innerHTML = LaReponse;
//document.getElementById("reponse").appendChild(LaReponse);
}
//-->
</script>
</head>

<body>
<p>
<button onclick="EnvoiRequete(LireReponse);">Afficher coucou</button>
<div id="reponse"></div>
</p>
</body>
</html>

ça marche ....
0
Vivi86
 
je te remercie c'est nikel
0