这是我用来从php文件中获取数据的ajax
$.ajax({
type: "post",
url: "content.php",
data: somevariable,
dataType: "text",
success: function(response) {
$(this).parent().find(".loadingdataimage").hide();
$content.html(response);
}.bind(this)
});
现在我怎样才能使这段代码在页面加载时从content.php文件中获取页面的初始数据
答案 0 :(得分:1)
@Octopoid是正确的解决方案,下面是一个使用JQuery如何做到这一点的例子:
$.ready(function () {
$.ajax({
type: "post",
url: "content.php",
data: somevariable,
dataType: "text",
success: function(response) {
$(this).parent().find(".loadingdataimage").hide();
$content.html(response);
}.bind(this)
});
});
如果你想将逻辑分成一个你可以再次调用的函数,你可以这样实现:
var refresh = function() {
$.ajax({
type: "post",
url: "content.php",
data: somevariable,
dataType: "text",
success: function(response) {
$(this).parent().find(".loadingdataimage").hide();
$content.html(response);
}.bind(this)
});
}
$.ready(refresh);