Search code examples
jqueryxmlhttprequestactivexobject

send post method with jquery


I've got this code, and i don't see where the problem is :

if (window.XMLHttpRequest || window.ActiveXObject) {
    var id = $(this).attr("id");
    alert("bon");
    xhr =getXMLHttpRequest();
    xhr.open("POST", "handlingData.php",true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("type="+$('input[name="type'+id+'"]').val()+"&titre="+$('input[name="titre'+id+'"]').val()+"&texte="+$('input[name="texte'+id+'"]').val()+"&reponse="+$('input[name="reponse'+id+'"]').val());

} 

what i wanted to do was to send data in a POST.

Here's getXMLHttpRequest() :

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("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
    return null;
}

return xhr;
}

Solution

  • Since you use jQuery you could use $.post

    // i'll show just to for example
    var data = { 
        type : $('input[name="type'+id+'"]').val(),
        titre : $('input[name="titre'+id+'"]').val()
    }
    $.post('handlingData.php', data, function(response) {
        // do something after the server responded
    });