-4

这个问题现在已经解决了,我不再需要帮助。

我有这段代码并且遇到了一些问题

$.ajax({
  url: this.html_url,
  cache: false,
  success: function(html){
        $('body:last-child').append(html);
        return true;
  }
});
doSomething();

我的问题是在ajax成功函数完成之前调用了doSomething。我希望在 ajax 调用完成后立即调用 doSomething,包括成功函数。有什么帮助吗?

4

4 回答 4

1

只需将其放在回调中!

$.ajax({
    url: this.html_url,
    cache: false,
    success: function(html) {
        $('body:last-child').append(html);
        doSomething();
    }
});
于 2013-08-11T10:07:03.703 回答
1

做这个:

$.ajax({
      url: this.html_url,
      cache: false,
      success: function(html){
            $('body:last-child').append(html);
            doSomething();
            return true;
      }
   });

或者使用承诺。

于 2013-08-11T10:07:18.753 回答
0

$.ajax是一个异步函数,所以你应该把你的doSomething函数放在success属性中。

于 2013-08-11T10:08:38.180 回答
0

您需要在成功中编写函数,如下所示。

$.ajax({
  url: this.html_url,
  cache: false,
  success: function(html){
        $('body:last-child').append(html);
        doSomething();
        return true;
  }
});
于 2013-08-11T10:12:43.400 回答