ajax在成功后再调用另一个js函数

时间:2016-04-19 16:51:12

标签: javascript jquery ajax

我正在尝试制作ajax帖子。我只是想学习如何在ajax成功之后调用一些函数。

我希望在ajax成功之后调用以下函数:

function col() {
        var $container = $(".post-users-body");
        $container.imagesLoaded(function() {
            $container.masonry({
                columnWidth: ".collectionPostWrap",
                itemSelector: ".collectionPostWrap"
            });
        });
}

ajax的帖子是:

$("body").on("click","#update_button",function() {
    var updateval = $("#update").val();
    var dataString = 'update=' + updateval ;
      $.ajax({
        type: "POST",
        url: "requests/post.php",
        data: dataString,
        cache: false,
        success: function(html) {
          $(".post-users-body").prepend(html);
          col();            
        }
      });

    return false;
  });

所以ajax帖子工作正常,但它没有调用col();函数。

我在这里做错了什么。谁能告诉我?

1 个答案:

答案 0 :(得分:1)

ajax返回一个promise对象。使用它并进行一些功能链接。

    <script>
        (function(){

            function col(){
                alert("col called");
            }

            $.ajax({
                type:"GET",
                url:"http://localhost:5850/api/someservice"}
             ).done(function(){
                    debugger;
                    col();
                });
        })()



    </script>