2

我有这段代码,但我看不出问题出在哪里:

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;
}
4

3 回答 3

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
});
于 2012-08-03T15:26:54.443 回答
0

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

于 2012-08-03T15:28:20.573 回答
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...
  }
});
于 2012-08-03T15:28:35.487 回答