2012-05-15 95 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

沒問題,很高興我可以幫忙:) –