SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast alokjain_lucky's Avatar
    Join Date
    Feb 2008
    Location
    India
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to handle jQuery XMLHttpRequest output

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

    Code JavaScript:
     $(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?

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $.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
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Enthusiast alokjain_lucky's Avatar
    Join Date
    Feb 2008
    Location
    India
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.

    I have updated code to following

    Code JavaScript:
     $(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.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    Code:
    function(data) {console.log(data);}
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $('.alexarank').each(function (i) {
              $.
    get("some.php", { galleryurl"http://www.mysite.com" }, 
                  function(
    data) {
                      $(
    '.alexarank').text(data);
                  });
            });
        }); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •