2010-12-07 3 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

+0

@ user469652 - 上記のように、[docsは 'success'コールバックのための署名と同じ](http://api.jquery.com/jQuery.ajax/):' func(data、textStatus、 XMLHttpRequest) 'のように、他のパラメータを必要としない場合は、上記のように' data'の値を1つだけ持ちます。 –

関連する問題