How to handle jQuery XMLHttpRequest output

I have used following code to get alexarank for a number of sites


 $(document).ready(function(){
  var $taglist = $('.alexarank');
    for( var i = 0, n = $taglist.length;  i < n;  ++i ) {
      $taglist[i].innerHTML = $.get("alexarank.php", { siteyurl: "http://www.mysite.com" });
    };
});

It returns [object XMLHttpRequest] as an output.

I am new to jQurey so i am not sure how can I get actual value returned by php file and put it inside html element?

$.get function is an asynchronous function which means that you need to write callback as explained on this page:
http://docs.jquery.com/Ajax/jQuery.get

BTW, you can use each function instead of the for loop:
http://docs.jquery.com/Core/each#callback

Thanks for your help.

I have updated code to following


 $(document).ready(function(){
  $('.alexarank').each(function (i) {
      $.get("alexarank.php", { galleryurl: "http://www.mysite.com" }, function(data) {console.log(this);} );
    });
});

In the console i get Object type=GET as output.

My question is still same, how can i display output.

alexarank is a class name used on a span tag, in which i want to display output of the ajax call.

i.e. output will be something like <span class=“alexarank”>12345</span>

I hope this make the requirement and problem clear.

Try:


function(data) {console.log(data);}


$('.alexarank').each(function (i) {
      	$.get("some.php", { galleryurl: "http://www.mysite.com" },
	  		function(data) {
	  			$('.alexarank').text(data);
	  		});
		});
	});