2016-09-20 14 views
0
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'); 
    } 
} 

".done"でrefreshCartを呼び出す必要があります。どうすれば ".done"にコールバック関数を書くことができますか?申し訳ありません、私はAjaxで新しいです。完了時にAjaxが別のAjax関数を呼び出す

答えて

1
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'); 
     } 
    ); 
    } 
} 

使用法:ここで

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を使用する方法の一例である

$.ajax({ 
 
    url: 'http://echo.jsontest.com/title/ipsum/content/blah', 
 
    method: 'GET' 
 
    }) 
 
    .done(function(response) { 
 
    console.log(response); 
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
を要求: refreshCart関数はオブジェクトである

場合にもこれを行うことができます

+0

私は、この関数refreshCartを呼び出す必要があります。出来ますか? –

+0

もちろん、関数として 'refreshCart'を渡すだけで、代わりに私の代わりにコールバックとして関数を渡す必要はありません。 – WalksAway

+0

.done関数で警告を出します。 alert( 'test'); \t アラートが機能していません。 –

1

このコードをクラス内で参照していることを前提としています。

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'); 
} 
0

のAjax機能:

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(url, data, refreshCart); 
関連する問題