2010-12-07 92 views
2

我知道这是某种回调概念,但不知道该怎么做。当成功数据到达jQuery时执行函数ajax

$.ajax({ 
     'url': '/test/', 
     'type': 'POST', 
     'data': {'age': age}, 
     'dataType': 'html', 
     'success': function(data, textStatus, xhr) { 

     //I want when the data arrives, then execute another function, because the function is too big to place here. 

     } 

}); 

回答

2

如果您需要执行一些其他的功能与的数据作为参数,这样做:

$.ajax({ 
    'url': '/test/', 
    'type': 'POST', 
    'data': {'age': age}, 
    'dataType': 'html', 
    'success': myFunction 
}); 

//then, defined anywhere that's in scope: 
function myFunction(data) { 
    //do something with data 
} 

如果你需要做一些工作然后通话该功能...做到这一点:

$.ajax({ 
    'url': '/test/', 
    'type': 'POST', 
    'data': {'age': age}, 
    'dataType': 'html', 
    'success': function(data) { 
    //do stuff... 
    myFunction(data); 
    } 
}); 
+0

那么什么是信号myFunction? – user469652 2010-12-07 00:38:29

相关问题