php jquery-如何在准备好的文档中运行ajax?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10602323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery- how to run ajax in document ready?
提问by Joe
I'm trying to load information with ajax when my page loads but no information is displaying, can anyone spot what I'm doing wrong?
当我的页面加载但没有显示任何信息时,我正在尝试使用 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 contains:
ajax_load.php 包含:
<?php
if(isset($_POST['artist'])) {
$artist = $_POST['artist'];
echo $artist;
}
echo "test";
?>
the html part of the page is fine
页面的 html 部分很好
回答by James Allardice
You need to change the value of the successoption to be a reference to a function:
您需要将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
});
});
Currently you are setting successto the return value of finished, which is undefined. If you check your browser console you will likely be getting an error along the lines of "undefined is not a function".
当前,您正在设置success的返回值finished,即undefined。如果您检查浏览器控制台,您可能会收到“未定义不是函数”这样的错误。

