在砌体布局中使用ajax加载网页的初始数据

时间:2015-09-12 19:22:32

标签: javascript php jquery ajax masonry

这是我用来从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文件中获取页面的初始数据

1 个答案:

答案 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);