1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
function appel_ajax_standard(url, params, options)
{
options = options || { }; // si option non passé on crée un objet vide
...
try
{
var xhr = getXhr();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if(options.onSuccess) options.onSuccess(xhr.responseText, xhr); // passer xhr peut aussi être pratique
}
else
{
if(options.onFailure) options.onFailure(xhr);
}
}
}
xhr.open("GET", (url + "?" + params), true);
xhr.send(null);
return true;
}
catch (e)
{
return false;
}
}
// exemple d'appel :
var monElement = document.getElementById(idElement);
var monUrl = "./repertoire/page.php";
var resultat_ajax = appel_ajax_standard(monUrl, "superParam=chouetteValeur", {
onSuccess: function(retour) {
monElement.appendChild(document.createTextNode(retour));
},
onFailure: ajaxErrorHandler
});
if (!resultat_ajax) { alert("Une défaillance ajax a empêché ce traitement de s'effectuer. Veuillez contacter votre ragondin d'appartement."); }
function ajaxErrorHandler(xhr) {
alert("Erreur Ajax " + xhr.status); // par exemple
} |