< Retour

ajax()

function getXMLHttpRequest() {
    var xhr = null;

    if (window.XMLHttpRequest || window.ActiveXObject) {
        if (window.ActiveXObject) {
        try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        } else {
        xhr = new XMLHttpRequest(); 
        }
    } else {
        alert("Ce navigateur ne supporte pas l'objet XMLHTTPRequest.");
        return null;
    }

    return xhr;
}

function ajax(method, async, callback, parameters, value){
    var post_string = "method=" + method;

    if (typeof parameters != "undefined") {
        if (parameters instanceof Array) {
            for (var i in parameters) {
                post_string += "&" + i + "=" + encodeURIComponent(parameters[i]);
            }
        } 
        else if (parameters !== null) {   
            post_string += "&" + parameters + "=" + encodeURIComponent(value);
        }
    }

    var xhr = getXMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var json = JSON.parse(xhr.responseText);
            callback(json);
        }
    }

    var url = "http://.............ajax_queries.php";

    xhr.open("POST", url, async);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(post_string);
}

function ajaxCallback(data) {

}