tags:

views:

27

answers:

2
$(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);
    });
});

The POST query is "undefined" (Firebug).

+3  A: 

You should use $.ajax - it will standardise the whole XmlHTTPRequest thing across browsers.

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

(And, BTW, if you setInterval in your click handler, you will start a new periodic call to your ajaxselectrss function every time the button is clicked.)

Also, your context has changed due to the wrapper function. Try changing your click handler like so:

$(".rshownews").click(function() {
  var _this = this;
  window.setInterval(function() {ajaxselectrss($(_this).attr("title"))}, 1000);
});
sje397
But with $.ajax is the same.
lam3r4370
What is the error?
sje397
function ajaxselectrss(rssurlvar) {$.ajax({ type: "POST", url: "rssget.php", data: "rurl=" + rssurlvar, success: function(msg){ alert( "Data Saved: " + msg ); } });}
lam3r4370
@sje397 - There isn't any error.The POST is undefined.
lam3r4370
"POST" here is a string...it can't be undefined. I don't understand.
sje397
Does putting 'rssget.php' in your browser address bar give you anything? You can look at how the data is coming back in Firebug or Chrome developer tools, and/or add an error handler (like the success one) to the `$.ajax` call.
sje397
rurl=undefined.From Firebug - "POST query"
lam3r4370
That would indicate that the rssurlvar you are passing in is undefined. Are you sure the `.rshownews` items have titles?
sje397
Check edit.....
sje397
Yes,it works!Can you explain me the difference(_this)
lam3r4370
A function call be called using the [`call`](http://www.devguru.com/technologies/ecmascript/quickref/call.html) function - which allows you to set what the 'this' parameter is inside the function. JQuery sets this to be the html DOM element, but in plain javascript it is normally just the function itself.
sje397
+1  A: 

Since you seem to use jquery anyway ($(document).ready) you could use it's wrapper for simplifying ajax-requests.

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

stefan