jquery post not returning data on success handler

Discussion in 'jQuery' started by danramosd, Mar 13, 2010.

  1. #1
    Im trying to do something very simple and its not working. I want to run a jquery post() and retrieve the html from the file the posting too. My jquery is
    
     $.ajax({
      url: 'test.php',
      success: function(data) {
        alert('Load was performed.'+data);
      }
    });
     
    Code (markup):
    The test.php file just has a simple
     <h1>this is a test</h1>
    Code (markup):
    . When i go to run the script though no data is returned.
     
    danramosd, Mar 13, 2010 IP
  2. inegoita

    inegoita Member

    Messages:
    54
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Hi,

    your code looks ok. I would also attach an error handler see if that is triggered

    Maybe you can also try the $.load or $.get functions insted of $.ajax see if that works

    kindest regards,
    John
     
    inegoita, Mar 14, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi, you have to echo data inside your php script to be able to see it:

    file test.php
    
    <?php echo "<h1>this is a test</h1>";?>
    PHP:
    Regards :)
     
    koko5, Mar 15, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    no?
    
    <h1>foo</h1>
    
    <?PHP
    
    echo "<h1>goo again</h1>";
    
    PHP:
    this would render it twice, the .php is treated as html when not after an open <? tag
    hence the problem is something else. look at the console in firebug to monitor the request and expand the response, make sure the right filepath/file gets called.
     
    dimitar christoff, Mar 15, 2010 IP
  5. inegoita

    inegoita Member

    Messages:
    54
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #5
    it doesn't matter if you echo the HTML from PHP or you write it directly in the file, AJAX should receive the end result so HTML
     
    inegoita, Mar 15, 2010 IP