AJAX不发送POST查询

时间:2010-08-03 10:14:39

标签: ajax post

$(document).ready(function() {
  function ajaxselectrss(rssurlvar) {
  var ajaxRequest;  // The variable that makes Ajax possible!

 try{
  // Opera 8.0+, Firefox, Safari
  ajaxRequest = new XMLHttpRequest();
 } catch (e){
  // Internet Explorer Browsers
  try{
   ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
   }
  }
 }
 // Create a function that will receive data sent from the server
 ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){

   var ajaxDisplay = document.getElementById('news');
   ajaxDisplay.innerHTML = ajaxRequest.responseText;
  }
 }



 //var rssurlvar = $(this).attr("title");
 var queryString = "rurl=" + rssurlvar;
 var urltofile = "rssget.php";
 ajaxRequest.open("POST", urltofile, true);
 ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 ajaxRequest.setRequestHeader("Content-length", queryString.length);
 ajaxRequest.setRequestHeader("Connection", "close");
 ajaxRequest.send(queryString); 

}
    $(".rshownews").click(function() {
        window.setInterval(function() {ajaxselectrss($(this).attr("title"))}, 1000);
    });
});

POST查询是“未定义”(Firebug)。

2 个答案:

答案 0 :(得分:3)

您应该使用$.ajax - 它将跨浏览器标准化整个XmlHTTPRequest事物。

$.ajax({
    type: "POST",
    url: "rssget.php",
    data: queryString,
    success: function(data) {
      $('#news').html(data);
    }
});

(而且,顺便说一下,如果您在点击处理程序中setInterval,则每次点击该按钮时,您都会开始对ajaxselectrss函数进行新的定期调用。)

此外,由于包装函数,您的上下文已更改。尝试更改点击处理程序,如下所示:

$(".rshownews").click(function() {
  var _this = this;
  window.setInterval(function() {ajaxselectrss($(_this).attr("title"))}, 1000);
});

答案 1 :(得分:1)

因为你似乎总是使用jquery($(document).ready)你可以使用它的包装来简化ajax请求。

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