Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax在完成后调用另一个Ajax函数_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript Ajax在完成后调用另一个Ajax函数

Javascript Ajax在完成后调用另一个Ajax函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我需要叫refreshCart进来“.done”。如何在“.done”中编写回调函数?对不起,我是Ajax新手 var App = { actionRequest: function (url,data,callback){ var that = this; $('#menu').panel('close'); $.mobile.loading('show'); $.when( $.ajax({ method: 'POST

我需要叫refreshCart进来“.done”。如何在“.done”中编写回调函数?对不起,我是Ajax新手

var App = {
actionRequest: function (url,data,callback){
    var that = this;
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            that.refreshCart();
            $.mobile.loading('hide');               
        }

    );
}

refreshCart: function(){        
    App.loadExternalContent('content','scripts/data_ajax.php','action=getCart','templates/cart.htm');
    }
}
用法:

如果对象中有函数
refreshCart
,也可以执行以下操作:

var object = {
  actionRequest: function(url, data, callback) {
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.ajax({
      method: 'POST',
      url: url + '?' + new Date().getTime(),
      data: data
    }).done(function(data, html) {
        if ($.isFunction(callback)) {
          callback();
        }
        $.mobile.loading('hide');
      }
    );
  }
}
下面是一个如何使用ajax请求的示例

$.ajax({
网址:'http://echo.jsontest.com/title/ipsum/content/blah',
方法:“获取”
})
.完成(功能(响应){
控制台日志(响应);
})
用法:

如果对象中有函数
refreshCart
,也可以执行以下操作:

var object = {
  actionRequest: function(url, data, callback) {
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.ajax({
      method: 'POST',
      url: url + '?' + new Date().getTime(),
      data: data
    }).done(function(data, html) {
        if ($.isFunction(callback)) {
          callback();
        }
        $.mobile.loading('hide');
      }
    );
  }
}
下面是一个如何使用ajax请求的示例

$.ajax({
网址:'http://echo.jsontest.com/title/ipsum/content/blah',
方法:“获取”
})
.完成(功能(响应){
控制台日志(响应);
})

我假设您在课堂上引用了此代码

var object = {
    actionRequest: function(url, data, callback) {
      var that = this;

      $('#menu').panel('close');
      $.mobile.loading('show');
      $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
          }).done(function(data, html) {
              // without using a callback
              that.refreshCart();
              $.mobile.loading('hide');
            }

          );
        },
        refreshCart: function() {
          App.loadExternalContent('content', 'scripts/data_ajax.php', 'action=getCart', 'templates/cart.htm');
        }
    }

我假设你在课堂上引用了这段代码

var object = {
    actionRequest: function(url, data, callback) {
      var that = this;

      $('#menu').panel('close');
      $.mobile.loading('show');
      $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
          }).done(function(data, html) {
              // without using a callback
              that.refreshCart();
              $.mobile.loading('hide');
            }

          );
        },
        refreshCart: function() {
          App.loadExternalContent('content', 'scripts/data_ajax.php', 'action=getCart', 'templates/cart.htm');
        }
    }
Ajax功能:

actionRequest: function (url,data,callback){
    var self = this;  //keep reference of current instance for more info read closures in JS
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            self.refreshCart(); 
            $.mobile.loading('hide');               
        }

    );
}

refreshCart: function(){        
    App.loadExternalContent('content','scripts/data_ajax.php','action=getCart','templates/cart.htm');
}
actionRequest: function (url,data,callback){
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            callback();
            $.mobile.loading('hide');               
        }

    );
}
调用函数:

actionRequest: function (url,data,callback){
    var self = this;  //keep reference of current instance for more info read closures in JS
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            self.refreshCart(); 
            $.mobile.loading('hide');               
        }

    );
}

refreshCart: function(){        
    App.loadExternalContent('content','scripts/data_ajax.php','action=getCart','templates/cart.htm');
}
actionRequest: function (url,data,callback){
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            callback();
            $.mobile.loading('hide');               
        }

    );
}
Ajax功能:

actionRequest: function (url,data,callback){
    var self = this;  //keep reference of current instance for more info read closures in JS
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            self.refreshCart(); 
            $.mobile.loading('hide');               
        }

    );
}

refreshCart: function(){        
    App.loadExternalContent('content','scripts/data_ajax.php','action=getCart','templates/cart.htm');
}
actionRequest: function (url,data,callback){
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            callback();
            $.mobile.loading('hide');               
        }

    );
}
调用函数:

actionRequest: function (url,data,callback){
    var self = this;  //keep reference of current instance for more info read closures in JS
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            self.refreshCart(); 
            $.mobile.loading('hide');               
        }

    );
}

refreshCart: function(){        
    App.loadExternalContent('content','scripts/data_ajax.php','action=getCart','templates/cart.htm');
}
actionRequest: function (url,data,callback){
    $('#menu').panel('close');
    $.mobile.loading('show');
    $.when(

        $.ajax({
            method: 'POST',
            url: url + '?' + new Date().getTime(),
            data: data
        })            

    ).done(function(data,html) {        
            callback();
            $.mobile.loading('hide');               
        }

    );
}

我需要调用这个函数refreshCart。有可能吗?当然,您只需将函数传递给refreshCart,或者看看我的备选方案,您不需要将函数作为回调传递。我在.done函数警报(“测试”)中添加了警报;但是警报不起作用。请看更新,现在.done函数正在运行correct@NicholasChew我在回答中包括了一个例子,我需要调用这个函数refreshCart。有可能吗?当然,您只需将函数传递给refreshCart,或者看看我的备选方案,您不需要将函数作为回调传递。我在.done函数警报(“测试”)中添加了警报;但是警报不起作用。请看更新,现在.done函数正在运行correct@NicholasChew我在回答中包括了一个例子