开发者

execute a function when the success data arrived in jquery ajax

I know that's some kind of callback concept, but have no idea how to do that.

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

         //I want when the data arrives, then execute another 开发者_Go百科function, because the function is too big to place here.

        }

});


If you need to execute only some other function with takes the data as a parameter, do this:

$.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
}

If you need to do some work then call that function...do just that:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜