用jquery发送post方法

时间:2012-08-03 15:22:29

标签: jquery xmlhttprequest activexobject

我有这个代码,我不知道问题出在哪里:

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());

} 

我想做的是在POST中发送数据。

这是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;
}

3 个答案:

答案 0 :(得分:3)

由于您使用jQuery,因此可以使用$.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
});

答案 1 :(得分:0)

您的xhr.send()行看起来像是GET请求。使用POST时,发送表单数据。由于您使用的是jQuery,请参阅文档:http://api.jquery.com/jQuery.post/

答案 2 :(得分:0)

为什么不使用jQuery post方法?你已经在使用jQuery了,不妨!

http://api.jquery.com/jQuery.post/

var data = { type: $('input[name="type'+id+'"]').val() }; // Fill this in with the data from your form.

$.ajax({
  type: 'POST',
  url: 'handlingData.php',
  data: data,
  success: function(data) {
      // Do processing here...
  }
});
相关问题