2012-05-15 52 views
1

我试图加载信息与Ajax当我的页面加载,但没有信息显示,任何人都可以发现我做错了什么?jquery-如何在文档准备好运行ajax?

$(document).ready(function(){ 
    $.ajax({          
     url: 'ajax_load.php',    
     type: "post",   
     data: "artist=<?php echo $artist; ?>", 
     dataType: 'html',     
     beforeSend: function() { 
      $('#current_page').append("loading.."); 
      }, 
     success: finished(html), 
    }); 
}); 

function finished(result) { 
    $('#current_page').append(result); 
}; 

ajax_load.php包含:

<?php 

if(isset($_POST['artist'])) { 
    $artist = $_POST['artist']; 
    echo $artist; 
} 

echo "test"; 

?> 

页面的HTML部分是好的

+1

ajax页面的html是什么?加载...是否正确显示? –

+1

您正在从ajax_load.php的帖子中收到'artist',但是在初始页面加载时在'<?php echo $ artist;?>中填充了php变量'$ artist'? –

+0

尝试使用'failure'处理程序进行调试 – paislee

回答

8

您需要更改success选项的值是一个函数的引用:

$(document).ready(function(){ 
    $.ajax({          
     url: 'ajax_load.php',    
     type: "post",   
     data: "artist=<?php echo $artist; ?>", 
     dataType: 'html',     
     beforeSend: function() { 
      $('#current_page').append("loading.."); 
      }, 
     success: finished //Change to this 
    }); 
}); 

当前您正在将success设置为返回n值为finished,即undefined。如果你检查你的浏览器控制台,你可能会遇到“未定义不是函数”的错误。

+0

非常感谢:)我开始学习C,所以我不希望以这种方式使用函数。干杯! – Joe

+2

没问题,很高兴我可以帮忙:) –